[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 ...
随机推荐
- 我一直记不住的vim用法
一.多行编辑进入visual block模式一般模式下Crtl+v组合键以块的形式选中待编辑的文本 进入visual line模式一般模式下大写V以行的形式选中待编辑的文本 上述两种模式的复制用y,删 ...
- selinux关闭
查看SELinux状态: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...
- JQuery动态添加多个tab页标签
jQuery是一个兼容多浏览器的js库,核心理念是write less,do more(写的更少,做的更多),jQuery使用户能更方便地处理HTML documents.events.实现动画效果, ...
- shell脚本学习(六)
shell函数 注:现在是unix编程 实例: #!/bin/shdemon(){ echo "这是一个shell脚本"}demon 注: 调用是只写函数名没有() 函数的返回值 ...
- tp3.23 nginx lnmp填坑
thinkphp3.23在apache上可以轻松实现4个路由模式 但是在nginx上就出现问题 我们的环境是用lnmp包实现(地址:https://lnmp.org/) 安装完成后,ta的lnmp的n ...
- mac MyEclipse2017 CI10安装破解心得
前段时间也不知弄了什么东西把之前的me弄坏了,于是看看新版本的情况,准备安装个新版本,一看出了ci10,安装之. 破解资源请到这里下载 https://download.csdn.net/downlo ...
- RedisDesktopManager-0.9.3 for windows (转)
redis数据库的可视化工具 官方出了RedisDesktopManager-0.9.8版本后要购买了.之前自用的Windows版本0.9.3.817有需要的可以使用.解压直接启动即可.主要以备自用! ...
- Codeforces #447 Div.2 Tutorial
Problem A:QAQ 给一个字符串,求出可非连续的QAQ序列有多少个. Analysis:比较水的一道题,记录每一个Q的位置,预处理A的个数即可 然而还是fst了,原因是未考虑一个Q都没有的极端 ...
- [xsy3241]暴风士兵
题意:一个血量为$h$的人,它会被攻击$n$次,第$i$次有$p$的概率$-1$滴血(每次的$p$不同),问每次攻击后他的血量期望,强制在线 若一个人被扣了$i$滴血的概率为$p_i$,那么记多项式$ ...
- 将字符串的编码格式转换为utf-8
方式一: /** * 将字符串的编码格式转换为utf-8 * * @param str * @return Name = new * String(Name.getBytes("ISO-88 ...