[Functional Programming Monad] Combine Stateful Computations Using A State Monad
2024-08-28 14:29:20
The true power of the State ADT really shows when we start combining our discrete, stateful transactions. We start looking at another construction helper named of
, used to lift any given value of any type into the resultant. We also explore another instance method called chain
that is used for combining our simple, discrete transactions into more complex flows that can be extended to meet our needs as they arise. To create a set of stateful transactions to be combined, we see how the get
and modify
construction helpers can be used to make simple, easy to read code.
const { constant, Pair, Unit, curry, objOf, compose, State, mapProps, prop, option } = require("crocks"); const { put, get, modify } = State; const add = x => y => x+y;
const inc = add(); // 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 compute = n =>
State.of(n)
.chain(addState)
.chain(incState) console.log(
compute()
.runWith()
)
最新文章
- 使用 jQuery 和 CSS3 制作滑动导航菜单
- JS魔法堂:再次认识Function.prototype.call
- IE盒模型
- Django1.9开发博客(9)- 用户认证
- JS图片加载失败显示默认图片
- 暑假集训(4)第三弹 -----递推(Hdu1799)
- google yeoman
- linq里的select和selectmany操作 投影运算
- Android开发Thread+Handler演示样本(打地鼠)
- C# WebBrowser 代理的使用
- Android灯光系统--深入理解背光灯
- 【bird-java】bird-java概述
- BZOJ 3450: Tyvj1952 Easy [DP 概率]
- 微信小程序入门(五)
- [SP1043] GSS1 - Can you answer these queries I
- Apigee 简介与简单试用
- 【配置】log4j.properties 详解与配置步骤
- android 获取sd卡根目录
- windows下docker的安装并使用
- thinkphp模型创建