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. 洛谷P4331 [BOI2004] Sequence 数字序列 [左偏树]

    题目传送门 数字序列 题目描述 给定一个整数序列 a1​,a2​,⋅⋅⋅,an​ ,求出一个递增序列 b1​<b2​<⋅⋅⋅<bn​ ,使得序列 ai​ 和 bi​ 的各项之差的绝对 ...

  2. Windows下的cd命令

    http://blog.sina.com.cn/s/blog_5e971b850100rtta.html 首先先了解下什么是驱动器,什么是盘符. 驱动器是指通过系统格式化并带有一个驱动器号的存储区域, ...

  3. Pythonic和语法糖

    Pythonic就是以Python的方式写出简洁优美的代码. 用Python独有的语法写Python语言. 知乎:https://www.zhihu.com/question/20244565 某博客 ...

  4. 2017广西邀请赛 Query on A Tree (可持续化字典树)

    Query on A Tree 时间限制: 8 Sec  内存限制: 512 MB提交: 15  解决: 3[提交][状态][讨论版] 题目描述 Monkey A lives on a tree. H ...

  5. Linux命令之ftp

    ftp [-pinegvd] [host] pftp [-inegvd] [host] 用户通过ftp这个程序来使用Internet上的标准文件传输协议(FTP).本程序允许用户向远端网站发送文件,或 ...

  6. Flask实战第60天:帖子分页技术实现

    编辑manage.py,添加测试帖子 @manager.command def create_test_post(): for x in range(1, 100): title = '标题{}'.f ...

  7. 解决Linux环境下安装xampp之后外部无法连接MySQL的问题

    在Linux系统下,开发PHP一般都是LAMP环境,对于开发环境来讲,没有必要花太大精力去单独配置LAMP环境,采用xampp一键安装包是一个很好的方式.在Linux系统上安装xampp的过程这里就不 ...

  8. 【POJ 2154】 Color (置换、burnside引理)

    Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...

  9. BZOJ 4884 [Lydsy2017年5月月赛]太空猫(单调DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4884 [题目大意] 太空猫(SpaceCat)是一款画面精致.玩法有趣的休闲游戏, 你 ...

  10. 搭建vsftpd

    安装完软件以后 1.建立用户 adduser ftp passwd 密码 2.修改vsftp.conf文件 anonymous_enable改为NO,阻止匿名上传 Anon_mkdir_write_e ...