[Functional Programming] Combine State Dependent Transactions with the State ADT (composeK to replace multi chian call)
When developing a Finite State Machine, it is often necessary to apply multiple transitions in tandem. To accomplish this in most Redux work flows requires at best, implementing multiple action handlers in separate reducers; or at worse, the need to duplicate logic for similar action handlers, sometime across multiple files. However when using a State ADT, we can easily combine these independent transitions by simply chain-ing multiple discrete transitions into one transaction. We demonstrate this interface by transitioning two portions of related state with one transaction.
const { curry, compose, State, mapProps, composeK } = require("crocks");
const { modify } = State;
const state = {
left: 8,
moves: 0
};
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 over = (key, fn) => modify(mapProps({ [key]: fn }));
const limitMoves = clampAfter(0, 8);
const decLeft = () => over("left", limitMoves(dec));
const incMoves = () => over("moves", limitMoves(inc));
// Then there are a series of chain functions, using composeK
/**
* replace:
* decLeft()
* .chain(decLeft)
* .chain(decLeft)
* .chain(decLeft)
* .chain(incMoves)
* .chain(incMoves)
*/
const applyMove = composeK(
incMoves, incMoves, decLeft, decLeft, decLeft
)
const res = applyMove()
.execWith(state);
console.log(res); //{ left: 5, moves: 2 }
Another example:
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} = require('crocks');
const {modify} = State;
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)))
console.log(
JSON.stringify(
selectCard('green-square').execWith(state),
null,
2
)
);
/*
// Using Ramda to implememnt the same logic
const {compose, map, propOr, when, propEq, mergeLeft} = require('ramda');
const markAsSelected = (id) => when(propEq('id', id), mergeLeft({selected: true}))
const over = (key, fn) => compose(map(fn), propOr([], key));
const selectCard2 = (id) => over('cards', markAsSelected(id))
console.log(
JSON.stringify(
selectCard2('green-square')(state),
null,
2
)
)*/
[Functional Programming] Combine State Dependent Transactions with the State ADT (composeK to replace multi chian call)的更多相关文章
- [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] 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 Monad] Combine Stateful Computations Using Composition
We explore a means to represent the combination of our stateful computations using familiar composit ...
- [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] 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 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 ...
- [Functional Programming] Rewrite a reducer with functional state ADT
For example we have a feature reducer like this: // selectCard :: String -> Action String export ...
随机推荐
- Pandas之Series
# Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引 import numpy as np impor ...
- 解决jquery与zepto等其它库冲突兼容的问题
解决jquery与zepto等其它库冲突兼容的问题;(function ($) { }) (jQuery); ;(function ($) { }) (Zepto); 在Bootstrap ...
- js判断变量类型,类型转换,
1.typeof 操作符 主要检测基础数据类型 var a="zhangqian"; var b=true; ; var d; var e=null; var f=new Obje ...
- CSS技巧----DIV+CSS规范命名大全集合
网页制作中规范使用DIV+CSS命名规则,可以改善优化功效特别是团队合作时候可以提供合作制作效率,具体DIV CSS命名规则CSS命名大全内容篇. 常用DIV+CSS命名大全集合,即CSS命名规则 D ...
- CString和string在unicode与非unicode下的相互转换(转)
原文转自 http://blog.csdn.net/u014303844/article/details/51397556 CString和string在unicode与非unicode下的相互转换 ...
- haskell处理JSON(aeson)
aeson是haskell的一个库,其实我也不太懂,不过大概是这样的: 定义一个类型 如 data Person = Person { firstName :: String , lastNa ...
- 自定义topo遇到的坑
错误:TypeError: __init__() got an unexpected keyword argument 'delay' 解决办法:在创建topo的地方加一个link=TCLink即可, ...
- Vue v-for嵌套数据渲染问题
Vue v-for嵌套数据渲染问题 问题描述: 由于在获取商品子分类的时候,同时需要获取子分类下的商品,那么多层的列表渲染就只能是第一层好用 问题原因: vue在处理多层的渲染的时候,不能直接用等号赋 ...
- POJ1273 Drainage Ditches (网络流)
Drainage Ditches Time Limit: 1000MS Memor ...
- (转)NSString 类的使用
官网连接地址:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/Creat ...