[Functional Programming Monad] Combine Stateful Computations Using A State Monad
The true power of the State ADT really shows when we start combining our discrete, stateful transactions. We start looking at another construction helper named of, used to lift any given value of any type into the resultant. We also explore another instance method called chain that is used for combining our simple, discrete transactions into more complex flows that can be extended to meet our needs as they arise. To create a set of stateful transactions to be combined, we see how the get and modify construction helpers can be used to make simple, easy to read code.
const { constant, Pair, Unit, curry, objOf, compose, State, mapProps, prop, option } = require("crocks");
const { put, get, modify } = State;
const add = x => y => x+y;
const inc = add();
// State s a -> State(x => Pair(a, x))
// 'get' return result apply to variable a
const addState = n =>
get(add(n))
const incState = n =>
modify(inc) // modify return Unit() in variable position, Pair( (), 3 )
.map(constant(n)) // to keep the vairable a, we can use constant to wrap a value into function, Pair( 12, 3 )
const compute = n =>
State.of(n)
.chain(addState)
.chain(incState)
console.log(
compute()
.runWith()
)
[Functional Programming Monad] Combine Stateful Computations Using A State Monad的更多相关文章
- [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] Apply Stateful Computations To Functions (.ap, .liftA2)
When building our stateful computations, there will come a time when we’ll need to combine two or mo ...
- Scalaz(17)- Monad:泛函状态类型-State Monad
我们经常提到函数式编程就是F[T].这个F可以被视为一种运算模式.我们是在F运算模式的壳子内对T进行计算.理论上来讲,函数式程序的运行状态也应该是在这个运算模式壳子内的,也是在F[]内更新的.那么我们 ...
- [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers
Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...
- [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 ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- [Functional Programming Moand] Update The State Of A State Monad (put)
Stateful computations require the ability for their state to change overtime. We take a look on one ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
随机推荐
- 五十八 数据库访问使用SQLite
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...
- YumRepo Error: All mirror URLs are not using
yum 安装软件出错,最后发现是网络被拦截导致.
- ubuntu安装过程记录
[DNS修改] 新下载的ubuntu 17.04 安装后DNS是指向谷歌DNS的,谷歌被屏蔽啦,所以无法解析域名.解决办法: ctrl+alt+t 启动终端 : sudo su 输入管理員密碼,或去 ...
- python selenium firefox 添加cookie add_cookie
from selenium import webdriver driver = webdriver.Firefox() driver.get('http://www.baidu.com') cooki ...
- Linux命令之killall
killall [选项] [-signal(信号)] [name] killall发送一条信号给所有允许任意指定命令的进程.如果没有指定信号名,则发送SIGTERM.信号可以是名字或数字,只有信号0( ...
- Python标准库:内置函数divmod(a, b)
本函数是实现a除以b,然后返回商与余数的元组. 如果两个参数a,b都是整数,那么会采用整数除法,结果相当于(a//b, a % b).如果a或b是浮点数,相当于(math.floor(a/b), a% ...
- 【BZOJ 3309】DZY Loves Math
http://www.lydsy.com/JudgeOnline/problem.php?id=3309 \[\sum_{T=1}^{min(a,b)}\sum_{d|T}f(d)\mu(\frac ...
- 【20181023T2】行星通道计划【二维BIT】
题面 [错解] 唉好像有规律啊(x2>x1,y2>y1) 唉好像是个偏序啊 然后上CDQ套树状数组 唉怎么大样例跑了十多秒啊 可能有问题吧-- 刷刷刷把T3写了,回来 唉怎么写了个memc ...
- [P2698][USACO12MAR]花盆Flowerpot
Link: P2698 传送门 Solution: 对于可行区间$[L,R]$,随着$L$的递增$R$不会递减 因此可以使用尺取法来解决此题:不断向右移动左右指针,复杂度保持线性 同时为了维护区间内的 ...
- BZOJ 2073 [POI2004]PRZ(状压DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2073 [题目大意] 任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时只 ...