[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 ...
随机推荐
- [04]JS获取文件大小方法
</pre><pre name="code" class="html"><input id="file" ty ...
- spring boot 2.0之后默认的连接池 HIkariCP介绍
HIkariCP链接池比之传统的Tomcat JDBC datasource .c3p0 datasource 等传统链接池优势太大,在获取链接释放链接,执行效率上面高出很多,这个产品的口号是“快速. ...
- bzoj4897 [Thu Summer Camp2016]成绩单
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4897 [题解] 第一次看这题想的是f[l,r]的区间dp发现仅记录这两个好像不能转移啊 会出 ...
- Altium Designer 网络连接方式Port和Net Label详解
1.图纸结构 图纸包括两种结构关系: 一种是层次式图纸,该连接关系是纵向的,也就是某一层次的图纸只能和相邻的上级或下级有关系:另一种是扁平式图纸,该连接关系是横向的,任何两张图纸之间都可以建 ...
- kubernetes 搭建集群内服务
nginx-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: webapp spec: replicas: 2 te ...
- ffmpeg muxer 参数简要说明
参数 值 说明 movflags MP4 Muxer 标记 rtphint 增加RTP的hint track empty_moov 初始化空的moov box frag_keyframe 在视频关键帧 ...
- 如何在natTable表格上添加双击事件
在项目当中,有时候需要双击表格中的某一行触发一个事件或者一次数据请求,这时候,我们就需要在表格中绑定相关事件,思路实际上很简单,添加一个绑定事件就ok了,那么怎么添加呢?简单实现如下: 1.创建绑定双 ...
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- 去除整站下载文件中的tppabs等冗余代码
用TeleprotUltra复制了一个网站,结果网页中出现了很多形如tppabs=””的冗余代码,点击vs中的“在文件中查找”图标,打开“查找和替换”对话框,转到“快速替换”,然后进行以下设置: “查 ...
- (1)php开篇常识
一.php PHP由zend公司开发维护,目前最高版本PHP7.1,官网地址http://php.net/ PHP从5.5开始不支持XP,没有PHP6这个版本 PHP是嵌入到HTML的脚本代码,格式: ...