[Functional Programming] Compose Simple State ADT Transitions into One Complex Transaction
State
is a lazy datatype and as such we can combine many simple transitions into one very complex one. This gives us a lot of control over how our state changes over time. In this lesson we will use this to our advantage and combine many transactions into one complex action. In the end only the final state will be reported which can be reasoned about as one transition.
const state = {
cards: [
{id: 'green-square', color: 'green', shape: 'square'},
{id: 'orange-square', color: 'orange', shape: 'square'},
{id: 'blue-square', color: 'blue', shape: 'triangle'}
],
left: 8,
moves: 0
} const {State, when, assign, map, mapProps, propEq, curry, compose} = require('crocks');
const {modify} = State; const inc = x => x + 1;
const dec = x => x - 1; const clamp = (min, max) => x => Math.min(Math.max(min, x), max);
const clampAfter = curry((min, max, fn) =>
compose(
clamp(min, max),
fn
)
); const limitMoves = clampAfter(0, 8); const decLeft = () => over("left", limitMoves(dec));
const incMoves = () => over("moves", limitMoves(inc)); const markSelected = id => assignBy(propEq('id', id), {selected: true})
const assignBy = (pred, obj) => when(pred, assign(obj));
const over = (key, fn) => modify(mapProps({ [key]: fn })); const selectCard = id => over('cards', map(markSelected(id))) const answer = (id) => State.of(id)
.chain(incMoves)
.chain(incMoves)
.chain(decLeft)
.chain(selectCard); console.log(
JSON.stringify(
answer('green-square').execWith(state),
null,
2
)
);
/*
{
"cards": [
{
"id": "green-square",
"color": "green",
"shape": "square"
},
{
"id": "orange-square",
"color": "orange",
"shape": "square"
},
{
"id": "blue-square",
"color": "blue",
"shape": "triangle"
}
],
"left": 7,
"moves": 2
}
*/
[Functional Programming] Compose Simple State ADT Transitions into One Complex Transaction的更多相关文章
- [Functional Programming] Combine Multiple State ADT Instances with the Same Input (converge(liftA2(constant)))
When combining multiple State ADT instances that depend on the same input, using chain can become qu ...
- [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 porti ...
- [Functional Programming] From simple implementation to Currying to Partial Application
Let's say we want to write a most simple implementation 'avg' function: const avg = list => { let ...
- [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] Write simple Semigroups type
An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...
- [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 ...
- [Functional Programming] Reader with Async ADT
ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of ...
- [Functional Programming ADT] Create State ADT Based Reducers (applyTo, Maybe)
The typical Redux Reducer is function that takes in the previous state and an action and uses a swit ...
- [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 ...
随机推荐
- clips 前端 js 动画 抛物线加入购物车
抛物线加入购物车的特效动画(支持ie7以上,移动端表现良好) 1.引用一个极小的jquery插件库 2.启动 设置 起点 终点 和完成后回调函数 1.插件地址 git-hub上的官方主页 ...
- Computer(hdu 2196)
题意:给出一棵树,求出每个点与距离它最远的点的距离. /* 树形DP 先把无根树转为有根树,对于一个节点i来说,与它相距最远的点有两种可能,一是在它的子树中,二是不在,我们分别用f[i][0]和f[i ...
- 高级数据查询SQL语法
接上一篇关系数据库SQL之基本数据查询:子查询.分组查询.模糊查询,主要是关系型数据库基本数据查询.包括子查询.分组查询.聚合函数查询.模糊查询,本文是介绍一下关系型数据库几种高级数据查询SQL语法, ...
- 自定义View Layout过程 (3)
目录 目录 1. 知识基础 具体请看我写的另外一篇文章:(1)自定义View基础 - 最易懂的自定义View原理系列 2. 作用 计算View视图的位置. 即计算View的四个顶点位置:Left.To ...
- 行为型设计模式之状态模式(State)
结构 意图 允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类. 适用性 一个对象的行为取决于它的状态, 并且它必须在运行时刻根据状态改变它的行为. 一个操作中含有庞大的多分支的条 ...
- knockout 表单绑定 要怎么Mapping才好
问题 之前有了解过knockout,学习过绑定语法,结合帮助文档,做个Demo倒也不成问题,但是部分地方很不爽,不知道是我的用法不对,还是功力不够. 比如说,标签里定义的data-bind属性名,必须 ...
- 培训补坑(day5:最小生成树+负环判断+差分约束)
补坑补坑((╯‵□′)╯︵┻━┻) 内容真的多... 一个一个来吧. 首先是最小生成树. 先讲一下生成树的定义 生成树就是在一张图上选取一些边,使得整个图上所有的点都连通. 那么我们要求的最小生成树有 ...
- MFC 实现打印机打印功能
Visual C++6.0是开发Windows应用程序的强大工具,但是要通过它实现程序的打印功能,一直是初学者的一个难点,经常有朋友询问如何在VC中实现打印功能,他们往往感到在MFC提供的框架内实现这 ...
- RxVolley报错:Caused by: java.lang.RuntimeException: RequestQueue-> DiskBasedCache cache dir error
Caused by: java.lang.RuntimeException: RequestQueue-> DiskBasedCache cache dir error 这是因为SD卡动态权限导 ...
- Resin4 自定义端口
1. Resin4配置文件发生了较大变化,分为: app-default.xml web应用配置 cluster-default.xml 集群配置 health.xml -- 非pro版不支持 r ...