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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [Functional Programming] Write simple Semigroups type

    An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...

  6. [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 ...

  7. [Functional Programming] Reader with Async ADT

    ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. c语言数组传递

    转自:http://blog.csdn.net/xgmiao/article/details/9570825 点击打开链接 数组作为函数实参: C语言中数组作为函数实参时,编译器总是将其解析为指向数组 ...

  2. spring in action学习笔记七:@Conditional注解的用法

    @Profile注解是@Conditional注解的一个例子.即@Profile也是用@Conditional注解来实现的. 必须让条件实现Condition这个接口. 下面的案例讲如果环境中有mag ...

  3. font-family 定义的最后为什么要加一句sans-serif

    定义font-family时,最好在最后加一个sans-serif,这样如果所列出的字体都不能用,则默认的sans-serif字体能保证调用; W3C建议字体定义的时候,最后以一个类别的字体结束,例如 ...

  4. Windows Server 2008允许多用户登录远程桌面

    远程桌面是windows管理员对服务器进行管理最常用的方式,默认情况下windows server服务器是只允许单个远程链接的,如何开启多个用户远程桌面,下面就来介绍.   工具/原料   windo ...

  5. opengl glEnableClientState() 和 glDisableClientState() 作用

    http://zhidao.baidu.com/link?url=c3m55lgpjhU1Rb7TEP-aTGQAX3-GrcBk5NaUC2UA1ZtQiCCtHJzB_KoG7pWvPEybfYv ...

  6. 【推荐】nodeJS后台守护进程-forever

    A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever) 本地执行: npm i ...

  7. 在ros下使用tf

    tf真是一个好东西,把坐标变换都简化了 首先tf需要有一个broadcaster #include <ros/ros.h> #include <tf/transform_broadc ...

  8. 针对各地项目icomet停止服务的临时处理办法

    1.编辑一个脚本 vi /usr/local/watchicomet.sh #!/bin/bash sn=`ps -ef | grep ./icomet-server | grep -v grep | ...

  9. HTML5面向对象的游戏开发简单实例总结

    在阅读一本HTML5游戏开发相关书籍时发现一个很好的例子,通过这个例子可以对面向对象的开发进行更深入的理解.这个对象要实现的是:将一个CSS sprite中的图像绘制到canvas中.首先创建一个Sp ...

  10. Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】

    A. Flipping Game     time limit per test 1 second memory limit per test 256 megabytes input standard ...