[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, ...
随机推荐
- 【lua】可变长参数
lua可变长参数 在lua中可以使用...表示可变长参数,在函数内通过表访问可变参数 function rest(...) -- 把可变参数放在表类 local args = { ... } prin ...
- 【剑指offer】面试题 29. 顺时针打印矩阵
面试题 29. 顺时针打印矩阵 题目描述 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 【剑指offer】面试题 10. 斐波那契数列
面试题 10. 斐波那契数列 题目一:求斐波那契数列的第n项 题目描述:求斐波拉契数列的第n项 写出一个函数,输入n,求斐波拉契(Fibonacci)数列的第n项.斐波拉契数列定义如下: C++ 实现 ...
- 解决lazarus 多线程报错问题
很多人都在Windows中使用线程技术,然后同样的代码移植到Linux下一运行就出错.一开始不解其中的奥妙.既然Lazarus提供了TThread类,也没注明非要在Windows下使用.没道理在Lin ...
- Nodejs项目重复文件扫描
项目地址:https://github.com/danielstjules/jsinspect 1.安装jsinspect npm install -g jsinspect 2.进入至项目目录 d c ...
- 创建一个支持ES6的Nodejs项目
文章来自于:https://www.codementor.io/iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o 第一步:创建项目文件夹并初始化 ...
- Storm基本概念以及Topology的并发度
Spouts,流的源头 Spout是Storm里面特有的名词,Stream的源头,通常是从外部数据源读取tuples,并emit到topology Spout可以同时emit多个tupic strea ...
- 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- 2017CCPC 杭州 J. Master of GCD【差分标记/线段树/GCD】
给你一个n个初始元素都为1的序列和m个询问q. 询问格式为:l r x(x为2or3) 最后求1~n所有数的GCD GCD:把每个数分别分解质因数,再把各数中的全部公有质因数提取出来连乘,所得的积就是 ...
- 【SQL】ORACLE在sqlplus中使用spool方式生成建表语句
在实际生产中有时我们需要将一张表的数据导入到另外一张表,如果有PLSQL,我们可以通过PLSQL工具将数据导出为sql脚本,然后再在另外一个数据库中执行这个脚本.但有时在实际生产中我们没有PLSQL这 ...