[Functional Programming Monad] Combine Stateful Computations Using Composition
We explore a means to represent the combination of our stateful computations using familiar composition. We dive into what makes this possible by talking about what are known as Kleisli Arrows and explore some interesting properties surrounding them.
Once we understand the basics of how our stateful computations are chained, we look at how we can enlist a special compose helper named composeK. Using composeK, we show how we can further remove a lot of the boilerplate sometimes used to combine stateful computations.
Code we have:
const { constant, composeK, Unit, curry, objOf, compose, State, mapProps, prop, option } = require("crocks");
const { put, get, modify } = State;
const add = x => y => x+y;
const inc = add();
const multipy = x => y => x * y;
// 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 mutiplyState = n =>
get(multipy(n));
const compute = n =>
State.of(n)
.chain(addState )
.chain(incState)
.chain(mutiplyState)
console.log(
compute()
.runWith() // Pair(, )
)
We want to compose some functions, for example:
const addState = n =>
get(add(n)) const incState = n =>
modify(inc)
.map(constant(n))
Into:
const addAndInc =
composeK(
incState,
addState
)
const compute = n =>
State.of(n)
.chain(addAndInc)
.chain(mutiplyState)
Here we are using composeK, because incState and addState they both return State Number, combine multi state opreation, we need to use composeK.
Another benifit we got from using composeK, is point-free function, because it will automaticlly lift the param into State.
// From
const compute = n =>
State.of(n)
.chain(addAndInc)
.chain(mutiplyState) // To:
const compute = n =>
addAndInc(n)
.chain(mutiplyState)
Means we don't need manully call 'State.of' anymore.
The same we can compose further:
// From
const compute = n =>
addAndInc(n)
.chain(mutiplyState) // TO:
const compute = composeK(
mutiplyState,
addAndInc
);
composeK takes care for us :D
--
const { constant, composeK, Unit, curry, objOf, compose, State, mapProps, prop, option } = require("crocks");
const { put, get, modify } = State;
const add = x => y => x+y;
const inc = add();
const multipy = x => y => x * y;
// 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 mutiplyState = n =>
get(multipy(n));
const addAndInc =
composeK(
incState,
addState
)
const compute = composeK(
mutiplyState,
addAndInc
);
console.log(
compute()
.runWith()
)
[Functional Programming Monad] Combine Stateful Computations Using Composition的更多相关文章
- [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 transact ...
- [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 ...
- [Functional Programming Monad] Refactor Stateful Code To Use A State Monad
When we start to accumulate functions that all work on a given datatype, we end up creating a bunch ...
- [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] Monad
Before we introduce what is Monad, first let's recap what is a pointed functor: A pointed functor is ...
- [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 ...
- [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 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 ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
随机推荐
- bzoj 1491
思路:先求出每两点之间的最短路,建出n个最短路径图,然后枚举起点终点和中间点,计算条数用到拓扑图dp... 看别人的方法很巧妙地用floyd在计算最短路的时候就可以直接计算条数啦... #includ ...
- CentOS7安装和配置samba
(1)samba简介 CIFS:通用的internet文件系统,windows和unix系统之间共享文件的一种协议;客户端主要是windows:支持多节点同时挂载以及并发写入 (2)samba主配置文 ...
- centos系统服务管理
系统服务管理工具: chkconfig(所有linux发行版都有),用法很简单,如下: usage: chkconfig --list [name] chkconfig --ad ...
- shell日志重定向到null
用输出重定向符号> 即可,格式如下:shell命令 >/dev/null 若要将标准错误输出也一并重定向,如下:shell命令 >/dev/null 2>&1这样就不管 ...
- OSError: libgfortran.so.3: cannot open shared object file: No such file or directory
运行程序遇到下面问题 OSError: libgfortran.so.3: cannot open shared object file: No such file or directory 安装yu ...
- 安装 gcc 编译器
1.安装编译工具 gcc.gcc-c++.make 注意解决依赖关系,推荐使用 yum 安装,若不能联网可使用安装光 盘做为 yum 源 1)编辑 yum 配置文件: Mount /dev/cdrom ...
- 《深入理解Android2》读书笔记(七)
接上篇<深入理解Android2>读书笔记(六) 广播接受者 注册 ContextImpl @Override public Intent registerReceiver(Broadca ...
- 在Spring Controller中将数据缓存到session
Servlet方案 在Controller的方法的参数列表中,添加一个javax.servlet.http.HttpSession类型的形参.spring mvc会 自动把当前session对象注入这 ...
- 洛谷——P2299 Mzc和体委的争夺战
P2299 Mzc和体委的争夺战 题目背景 mzc与djn第四弹. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过前三弹的都知道).但如此之多的男家丁吸引来了我们的体委(矮胖小伙),他要来 ...
- 35、Flask实战第35天:权限设计
二进制及其相关运算 认识二进制 0,1,2,3,4,5,6,7,8,9,10:逢10进1 0,1:逢2进1 二进制转十进制 十进制 二进制 0 0 1 1 2 10 3 11 4 100 255 11 ...