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

  1. [Functional Programming Monad] Combine Stateful Computations Using Composition

    We explore a means to represent the combination of our stateful computations using familiar composit ...

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

  3. Scalaz(17)- Monad:泛函状态类型-State Monad

    我们经常提到函数式编程就是F[T].这个F可以被视为一种运算模式.我们是在F运算模式的壳子内对T进行计算.理论上来讲,函数式程序的运行状态也应该是在这个运算模式壳子内的,也是在F[]内更新的.那么我们 ...

  4. [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers

    Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...

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

  6. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  7. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

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

  9. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

随机推荐

  1. Python基础系列----字典、基本语句

    1.定义                                                                                               映 ...

  2. git 命令小结

    一.git 版本管理 1.git log: 获取当前版本之前的所有操作 2.git log --pretty=oneline:获取当前版本的前三和后三个操作 3.git reflog :获取当前项目下 ...

  3. bzoj 1491

    思路:先求出每两点之间的最短路,建出n个最短路径图,然后枚举起点终点和中间点,计算条数用到拓扑图dp... 看别人的方法很巧妙地用floyd在计算最短路的时候就可以直接计算条数啦... #includ ...

  4. CentOS上使用yum安装Apache

    关键词 CentOS上使用yum安装Apache 摘要 Apache在Linux系统中,其实叫“httpd”,它“无耻的”占据了官方名义!CentOS可以使用yum命令,非常简单和容易的安装Apach ...

  5. 转:攻击JavaWeb应用[2]-CS交互安全

    转:http://static.hx99.net/static/drops/tips-164.html 攻击JavaWeb应用[2]-CS交互安全 园长 · 2013/07/08 14:54 注:本节 ...

  6. KMP字符串匹配模板代码

    洛谷的模板传送门 #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> ...

  7. 颜色分类(LintCode)

    颜色分类 给定一个包含红,白,蓝且长度为n的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数0,1和2分别代表红,白,蓝. 样例   注意 不能使用代码 ...

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

  9. luogu P1016 旅行家的预算

    题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  10. 2017 icpc 西安网络赛

    F. Trig Function 样例输入 2 0 2 1 2 2 样例输出 998244352 0 2 找啊找啊找数列和论文.cosnx可以用切比雪夫多项式弄成(cosx)的多项式,然后去找到了相关 ...