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. (error) DENIED Redis is running in protected mode because protected mode is enabled

    在通过Java程序链接配置好的redis服务时出现 DENIED Redis is running in protected mode because protected mode is enable ...

  2. 【ASP.NET MVC】 路由机制:命名路由

    首先看一下命名路由和没有命名的差别: 命名路由: routes.MapRoute( name: "Test", // Route name url: "code/p/{a ...

  3. CSS3主要的几个样式笔记

    1.边框:border-color:    设置对象边框的颜色.     使用CSS3的border-radius属性,如果你设置了border的宽度是X px,那么你就可以在这个border上使用X ...

  4. SSH项目中遇到的问题

    1. Struts has detected an unhandled exception: Messages: antlr.collections.AST.getLine()I File: org/ ...

  5. dijkstra算法模板及其用法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  6. CSU - 1334 -好老师(STL-map用法)

    https://cn.vjudge.net/contest/157163#problem/E #include<map> #include<queue> #include< ...

  7. [BZOJ2337][HNOI2011]XOR和路径(概率+高斯消元)

    直接不容易算,考虑拆成位处理. 设f[i]表示i到n的期望路径异或和(仅考虑某一位),则$f[y]=\sum\limits_{exist\ x1\to y=0}\frac{f[x1]}{d[x1]}+ ...

  8. [xsy3241]暴风士兵

    题意:一个血量为$h$的人,它会被攻击$n$次,第$i$次有$p$的概率$-1$滴血(每次的$p$不同),问每次攻击后他的血量期望,强制在线 若一个人被扣了$i$滴血的概率为$p_i$,那么记多项式$ ...

  9. PHP登录(连接数据库)小案例

    实现效果               数据库信息  代码示例: 1. login.php <!DOCTYPE html> <html> <head> <met ...

  10. Hiho---欧拉图

    欧拉路·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现 ...