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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [Functional Programming] Monad

    Before we introduce what is Monad, first let's recap what is a pointed functor: A pointed functor is ...

  6. [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 ...

  7. [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers

    Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...

  8. [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 ...

  9. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

随机推荐

  1. node-java模块

    node-java模块 node-java使得开发人员,可以调用java优秀的jar包资源.有些方法逻辑,可能node不容易实现,但是java就可以很方便去做.这个时候,就可以使用node-java这 ...

  2. 前端读者 | CSS三角形和饼图

    @羯瑞 三角形 .triangle{width:0;height:0;border-width:50px;border-style:solid;border-color:red blue green ...

  3. 阿里云Maven仓库配置,Maven镜像配置

    Jenkins通过maven对java代码打包编译时,速度太慢,所以修改为阿里的Maven仓库 修改如下: [root@7mini-node2 conf]# vim /software/apache- ...

  4. 《深入理解Android2》读书笔记(四)

    接上篇<深入理解Android2>读书笔记(三) ActivityManagerService(AMS) 1.AMS由ActivityManagerNative(AMN)类派生,并实现Wa ...

  5. phpstorm如何进行文件或者文件夹重命名

    1.phpstorm的重构 1.1重命名 在phpstorm中,右键点击我们要进行修改的文件,然后又一项重构,我们就可以进行对文件的重命名. 接下来点击重命名进行文件或者文件夹的重新命名. 在框中输入 ...

  6. jQuery文档处理

    1.wrap 把所有匹配的元素用其他元素的结构化标记包裹起来.(我的理解就是给匹配的元素穿一件衣服) 把所有的段落用一个新创建的div包裹起来 $("p").wrap(" ...

  7. 【JAVA】在线程里使用线程外的变量为什么一定要是final类型

    这个情况真的碰到很多,开始的时候也很难理解,但是既然IDE提示要final那我就final咯,跑通就行管那么多呢.然而这并不是科学的学习方法,万一面试问你呢那不是倒了大霉. OK,看了一些

  8. Android异步消息处理机制(多线程)

    当我们需要执行一些耗时操作,比如说发起一条网络请求时,考虑到网速等其他原因,服务器未必会立刻响应我们的请求,如果不将这类操作放在子线程里去执行,就会导致主线程被阻塞住,从而影响用户对软件的正常使用. ...

  9. java 抽象方法 能用 静态 static 修饰,或者 native 修饰 么

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha static与abstract不能同时使用 用static声明方法表明这个方法在不生成类 ...

  10. Codeforces 555 B. Case of Fugitive

    \(>Codeforces \space 555 B. Case of Fugitive<\) 题目大意 : 有 \(n\) 个岛屿有序排列在一条线上,第 \(i\) 个岛屿的左端点为 \ ...