[Functional Programming] Define Discrete State Transitions using the State ADT
We build our first state transactions as two discrete transactions, each working on a specific portion of the state. Each of these transitions are governed by an acceptable range and we want to be able to apply these limit within our model.
With our discrete transactions we see that is easy to reason about and keeps our rules local to the data being worked on, without having to be considered in other transitions or, even worse, in the eventual view layer. Patterns like this make it easier to easily transport a given data model to any view layer.
const { curry, compose, State, mapProps } = require("crocks");
const { modify } = State;
const state = {
left: 8,
moves: 0
};
const inc = x => x + 1;
const dec = x => x - 1;
// Make sure 'x' in range of [min, max]
// if x < min, return min
// if x > max, return max
const limitRange = (min, max) => x => Math.min(Math.max(min, x), max);
// Run the 'fn', get the value, then pass into limitRange
const limitAfter = curry((min, max, fn) =>
compose(
limitRange(min, max),
fn
)
);
// For each key we passed in, apply fn to it
const over = (key, fn) => modify(mapProps({ [key]: fn }));
const limitMoves = limitAfter(0, 8);
const decLeft = () => over("left", limitMoves(dec));
const res = decLeft()
.chain(decLeft)
.execWith(state);
console.log(res); //{left: 6, moves: 0}
[Functional Programming] Define Discrete State Transitions using the State ADT的更多相关文章
- [Functional Programming] Read and Transform Values from a State ADT’s State (get)
Many times we need to access and transform state, either in part or in full, to be used when calcula ...
- [Functional Programming Monad] Combine Stateful Computations Using A State Monad
The true power of the State ADT really shows when we start combining our discrete, stateful transact ...
- [Functional Programming ADT] Adapt Redux Actions/Reducers for Use with the State ADT
By using the State ADT to define how our application state transitions over time, we clear up the ne ...
- [Functional Programming] Introduction to State, thinking in State
Recently, I am learning Working with ADT. Got some extra thought about State Monad. Basiclly how to ...
- [Functional Programming Monad] Modify The State Of A State Monad
Using put to update our state for a given state transaction can make it difficult to modify a given ...
- [Functional Programming Monad] Map And Evaluate State With A Stateful Monad
We explore our first stateful transaction, by devising a means to echo our state value into the resu ...
- [Functional Programming ADT] Initialize Redux Application State Using The State ADT
Not only will we need to give our initial state to a Redux store, we will also need to be able to re ...
- [Functional Programming Moand] Update The State Of A State Monad (put)
Stateful computations require the ability for their state to change overtime. We take a look on one ...
- [Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)
We take a closer look at the get construction helper and see how we can use it to lift a function th ...
随机推荐
- 为Ubuntu Gnome环境创建桌面快捷方式
为Ubuntu Gnome环境创建桌面快捷方式 安装gnome-panel包 sudo apt-get install --no-install-recommends gnome-panel 启动终端 ...
- sql 取一张表的全部外键
select a.name as 约束名, object_name(b.parent_object_id) as 外键表, d.name as 外键列, object_name(b.reference ...
- 理想中的SQL语句条件拼接方式 (二)
问题以及想要的效果,不重复叙述,如果需要的请先看 理想中的SQL语句条件拼接方式 . 效果 现在有2个类映射数据库的2张表,结构如下: public class User { public int U ...
- 【Git】GitHub的SSH提交配置[
Git可以通过https方式和ssh方式连接服务器上的仓库. 两者比较: 1.https: 比较方便,但是每次fetch和push代码都需要输入账号和密码,略显麻烦 2.ssh: 传输前压缩数据,传输 ...
- 杭电oj2032、2040、2042、2054、2055
2032 杨辉三角 #include <stdio.h> int main(){ ][],i,j,n; while(~scanf("%d",&n)){ ;i& ...
- 基于Xen实现一种domain0和domainU的应用层数据交互高效机制
项目里有一个需求,domain0的应用层需要定时给domainU(hvm windows)的应用层传递一块数据,原来的方案是在domainU的应用层架设一个http服务器,监听在某个端口,然后需要塞数 ...
- (八)ubuntu安装软件提示:Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
question: ubuntu@ubuntu:/usr/src/Linux-headers-3.5.0-23-generic$ sudo apt-get install cheese [sudo] ...
- [Oracle] DataGuard switchover
Oracle DataGuard switchover 2013/07/11 Tag.Data Guard,primary,standby,switchover 切换前primary site和sta ...
- 集合类---List
一.ArrayList详解 1.继承关系 public class ArrayList<E> extends AbstractList<E> implements List&l ...
- Kubernetes DNS安装配置
#镜像在谷歌上需要FQ###################################下载kubernetes#DNS#cd kubernetes/cluster/addons/dns#修改tr ...