[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, ...
随机推荐
- 【ASP.NET】必须知道的ASP.NET核心处理
该找工作了,回头复习一下基础知识,增加一下理论知识的理解,下面我们看一下今天要说的. ASP.NET应用程序对象 一.请求的处理参数:上下文对象HttpContext 当一个请求到达ASP.NET服务 ...
- hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)
水道题目,比赛时线段树写挫了,忘了RMQ这个东西了(捞) #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an int ...
- 【转载】RecyclerView使用全解析
崇拜下鸿洋大神,原文地址:http://blog.csdn.net/lmj623565791/article/details/45059587 概述 RecyclerView出现已经有一段时间了,相信 ...
- Codeforces 补题记录
首先总结一下前段时间遇到过的一些有意思的题. Round #474 (Div. 1 + Div. 2, combined) Problem G 其实关键就是n这个数在排列中的位置. 这样对于一个排 ...
- Friends number NBUT - 1223 (暴力打表)
Paula and Tai are couple. There are many stories between them. The day Paula left by airplane, Tai s ...
- Knockout.js(二):监控数组属性(Observables Arrays)
如果想发现并响应一个对象的变化,就应该使用监控属性(observables),如果想发现并响应一个集合的变化,就需要使用监控属性数组(observableArray).在很多情况下,它都非常有用,比如 ...
- hdu 4489 The King’s Ups and Downs(基础dp)
The King’s Ups and Downs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- [TCO2013]LitPanels
题意:一个$n\times m$的无色网格,你可以在其中选择两个$x\times y$的子矩形并在其中将其中任意的格子涂上颜色,问最终能得到多少种不同的网格 做这题会用到一个概念叫包围盒(boundi ...
- [xsy2282]cake
题意:一个$n\times n$的有标号点阵,现在用一条直线把它们分成两部分,问有多少种不同的分法 结论:方案数就是以点阵上的点为端点且不经过第三个点的线段数 对一个满足要求的线段,将其绕中点顺时针转 ...
- Don't know how to define struct flock on this system, set --enable-opcach=no
解决方法一: 最后查看php官方文档解决. 在configure 里面加上 --with-libdir=lib64 解决方法二: 编辑/etc/ld.so.conf 根据系统,加入include ...