[Functional Programming Monad] Modify The State Of A State Monad
2024-08-25 03:42:38
Using put
to update our state for a given state transaction can make it difficult to modify a given state based on its previous value. In this lesson we will explore a means to lift specialized functions that can be used modify our state’s value. These specialized functions must have the same type in their input and output. We take a look at a construction helper named modify
that is used to remove the boilerplate of this type of construction. We also start to see how we can extend simple discrete stateful transactions to create other stateful transactions.
Modify state is apply a function to the original state to get new state:
// modifyState :: (s -> s) -> State s ()
const modifyState = fn => State(s => Pair(Unit(), fn(s)));
// modifyState :: (s -> s) -> State s ()
const modifyState = fn => State(s => Pair(Unit(), fn(s)));
const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modifyState(mapProps({bubbles: add()}))
.runWith(bubble) // Pair( (), { bubbles: 41 } )
)
Of couse, we have the 'modify' function already in the library, so the code can be changed to:
const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modify(mapProps({bubbles: add()}))
.runWith(bubble) // Pair( (), { bubbles: 41 } )
)
最新文章
- [2016.01.22]万峰文本处理专家 v2.1
- IO-01. 表格输出(5)
- C语言文件方式输入与输出(最简洁方便实用的一种方式)
- [Effective JavaScript 笔记]第65条:不要在计算时阻塞事件队列
- 简单介绍.Net3.0 中跨线程访问控件
- Spring(五)AOP简述
- oracle sql语言模糊查询--通配符like的使用教程
- VS2013 不能打开DTCMS项目 的解决办法
- CQRS学习——Cqrs补丁,async实验以及实现[其二]
- 软碟通UltraISO 9.65.3237官方注册版
- CSAPP 六个重要的实验 lab5
- Swift 3.0 字符串、数组、字典的使用
- SQL Server删除表信息的三种方法
- Java基础---Java---正则表达式-----匹配、切割、替换、获取等方法
- javascript的隐式类型转换
- 在Ubuntu 12.04下创建eclipse的桌面链接
- python 将文件描述符包装成文件对象
- SDN 第三次上机作业
- 【Shell】Linux的判断表达式:-d,-f,-e等
- javascript开发HTML5游戏--斗地主(单机模式part2)