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. 自建yum源(只演示nginx服务,其它都一样)

    (1)概述 (2)yum server端配置 1)关闭防火墙和selinux systemctl stop firewalld systemctl disable firewalld sed -ri ...

  2. COCOS2D - JS 之JSON 解析

    list 类型的json数据  var source = ["10004","1234","4","3","1 ...

  3. CodeForces 779B Weird Rounding

    简单题. 删去结尾的不是$0$的数字,保证结尾连续的$k$个都是$0$,如果不能做到,就保留一个$0$. #include<map> #include<set> #includ ...

  4. Spring MVC源码——Root WebApplicationContext

    目录 Spring MVC源码--Root WebApplicationContext 上下文层次结构 Root WebApplicationContext 初始化和销毁 ContextLoaderL ...

  5. 【UOJ #179】线性规划 单纯形模板

    http://uoj.ac/problem/179 终于写出来了单纯性算法的板子,抄的网上大爷的qwq 辅助线性规划找非基变量时要加个随机化才能A,我也不知道为什么,卡精度吗? 2017-3-6UPD ...

  6. BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...

  7. 【构造】【贪心】hdu6090 Rikka with Graph

    给你n个点,让你连m条边,使得任意两两点对之间的最短路的和最小(两点若不可达,最短路记作n). 初始时ans=n*n*(n-1). 先尽量连成菊花图,每连一次让答案减小2*((n-2)*(i-1)+( ...

  8. ubuntu16安装navicat字体显示不正常,显示方框以及字体倒立

    昨天遇到了这个问题,网上找了很多方法都没有真正解决这个问题. 目前其他博客论坛说的主要方法有 1)将安装目录下的./start_navicat中的字符集改为zh_CN.UTF-8 2)将系统的默认字符 ...

  9. 错误:Caused by:org.apache.spark.SparkException: Kryo serialization failed: Buffer overflow.Available: 0, required: 21. To avoid this,

    这个是写入Redis时用的序列化器,然后错误提示是超过了大小限制,把配置调大即可. .set("spark.kryoserializer.buffer.max","128 ...

  10. Ubuntu 16.04搭建原始Git服务器

    说明:不要把有限的生命浪费到权限斗争中! 1.安装SSH sudo apt-get install openssh-server sudo service ssh start 2.安装Git sudo ...