[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 ...
- Aras Innovator DB备份与还原
错误信息 确认到该问题是因为孤立帐号的问题,在解决孤立帐号之前,可以通过语句查看,另外,还原了DB后,系统不会自动创建原来的登陆帐号的,需要手动新增登陆帐号 #查看孤立帐号列表exec sp_chan ...
- AtCoder Regular Contest 103 Problem D Robot Arms (构造)
题目链接 Problem D 给定$n$个坐标,然后让你构造一个长度为$m$的序列, 然后给每个坐标规定一个长度为$m$的序列,ULRD中的一个,意思是走的方向, 每次从原点出发按照这个序列方向,每 ...
- 制作启动U盘
概述 将普通的u盘制作成启动u盘,用于引导安装操作系统. 材料: 普通U盘 需要有足够的存储空间,里面的内容请提前备份. 操作系统iso文件 PowerISO 商业软件,有试用期:用来制作启动u盘 正 ...
- 【ModelMap】jsp中显示springmvc modelmap传递的对象
最近在做一个小网站,功能非常基础,决定用springmvc搭建. 遇到一个问题,在controller向前端传值时,比如使用ModelMap传了一个字符串,modelmap.addattribute( ...
- 解决phpStudy启动网站警告问题
在用phpStudy的时候,在页面中会有一些警告 notice:Undefined variable... notice:Undefined index... 在php.ini里面找到 display ...
- python队列、线程、进程、协程(转)
原文地址: http://www.cnblogs.com/wangqiaomei/p/5682669.html 一.queue 二.线程 #基本使用 #线程锁 #自定义线程池 #生产者消费者模型(队列 ...
- Java中Object的方法
构造方法摘要Object() 方法摘要protected Object clone() 创建并返回此对象的一个副本.boolean equals(Object ...
- luogu P1979 华容道
solution 被假hash可了半天....sadQAQ code // luogu-judger-enable-o2 #include<queue> #include<cstdi ...
- [Codeforces #174] Tutorial
Link: Codeforces #174 传送门 A: 求原根的个数,有一条性质是原根个数为$\phi(\phi(n))$,多了一个不会证的性质 如果要确定哪些是原根的话还是要枚举,不过对于每个数不 ...