[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, ...
随机推荐
- (翻译)在 Xamarin 应用中使用 MongoDB
原文地址:https://blog.xamarin.com/write-apps-using-mongodb-xamarin/ 在设计应用时,最重要的决定之一就是要使用什么类型的数据库. 不久之前,这 ...
- 关于在C#中对函数重载理解
函数重载是个什么概念,才接触的这个概念的时候我也是完全昏了,还在自己看看了书后就理解了.那什么是函数重载呢?我个人理解的是在同一个作用域下有多个同名的函数,但是他们的形参的类型是不同的,或者参数个数是 ...
- gulp-基本功能总汇
研究了三天的gulp,今天做一个结束吧. 本次包含的功能有: html压缩 图片压缩 css压缩 js检测 js压缩 文件合并 文件更名 提示信息 编译less 创建服务器-浏览器实时刷新 因为我安装 ...
- 【ASP.NET MVC】提高页面加载速度:脚本优化
在这里我们说一下脚本优化的三个方法: 一.在我们做Web开发的时候,当我们引用Js文件的时候,我们一般会将js文件放在文档的head标签中,这时当页面加载的时候,浏览器会按着由上到下的顺序,当浏览器遇 ...
- 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化
第6章 HBase API 操作6.1 环境准备6.2 HBase API6.2.1 判断表是否存在6.2.2 抽取获取 Configuration.Connection.Admin 对象的方法以及关 ...
- 德州扑克AI
德州扑克: 1:outs数,就是所听的牌的数量. 例子: 1:听顺子 4567 outs数就是8,能够成顺子的牌为3和8. 5689 outs数就是4,能够成顺子的牌只有7. 2:听同花 35 ...
- UGUI的优点新UI系统三效率高效果好
UGUI的优点新UI系统三效率高效果好 通过对批处理(batching).纹理图集(texture atlasing)和新的canvas组件的支持,新UI系统提供了一个经过优化的解决方案,使得开发者添 ...
- android 内存分哪些区
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com android 内存分哪些区 内存分哪些区 ============ 内存分为的5大区 1.栈区 ...
- 开发Android逆向工具
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 源代码及演示程序,请点击这里下载 下载地址: [北方网通] [电信网通] [下载说明] ...
- JZYZOJ1454 NOIP2015 D2T3_运输计划 二分 差分数组 lca tarjan 树链剖分
http://172.20.6.3/Problem_Show.asp?id=1454 从这道题我充分认识到我的脑子里好多水orz. 如果知道了这个要用二分和差分写,就没什么思考上的难点了(屁咧你写了一 ...