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. python登录知乎

    #coding:utf-8 import requests import urllib3 import time class Zhihu: def __init__(self): self.login ...

  2. 7月10日day2总结

    今天学习过程和小结 上午: java项目建立方法,maven Tomcat的部署和MySQL的安装与权限. 分配时中datanote和namenote DataNode 作用 1Data Node以数 ...

  3. mysql case when使用记录

    两种实现方式 第一种,CASE后面跟字段,当等于WHEN后面的值时,输出指定的数据 SELECT CASE gc.cat_id THEN '台球' THEN '羽毛球' ELSE '其它' END A ...

  4. 【转】beyond compare 启动提示“应用程序发生错误”

    [转]beyond compare 启动提示“应用程序发生错误”   今天到公司BCompare不能打开,重新安装也不能打开.最后处理下,就解决了.方法是把C:\Documents and Setti ...

  5. 原生dialog

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. LVM更换硬盘

    #检测坏道 smartctl -a /dev/sdd #硬盘检测 e2fsck -f /dev/mapper/vg_root-lv_data #重新定义空间大小,将原来的大小上减去要移走的硬盘 res ...

  7. 2.OpenStack-安装消息队列服务

    安装消息队列服务(安装在控制器上) yum install rabbitmq-server -y systemctl start mariadb.service 配置消息队列服务 systemctl ...

  8. Laravel中setAttribute和queryScope的用法

    setAttribute使用场景: 数据在存入数据库的时候需要进行预先处理,每次都会写很多重复代码,使用 setAttribute之后就可以在数据填充时自动完成. setAttribute的写法:se ...

  9. [BZOJ2223][BZOJ3524][Poi2014]Couriers 主席树

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2436  Solved: 960[Submit][St ...

  10. 51nod 1086 背包问题 V2 【二进制/多重背包】

    1086 背包问题 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放 ...