[Functional Programming Moand] Update The State Of A State Monad (put)
Stateful computations require the ability for their state to change overtime. We take a look on one way to replace the state portion of our stateful datatype with a value. We explore the mechanics of how this can be accomplished and introduce the put
construction helper. We also see how we can run our instances with execWith
to extract the state portion and discard the resultant.
Put state is replace the old state with new state:
//putState :: s -> State s ()
const putState = state => State(() => Pair(Unit(), state));
console.log(
putState('new')
.runWith('OLD') //Pair( (), "put" )
)
This is useful when we want to create 'reset':
//putState :: s -> State s ()
const putState = state => State(() => Pair(Unit(), state));
//reset :: () -> State String ()
const reset = () => putState('default');
console.log(
reset()
.execWith('OLD') // default
)
This opreation is commonly used, therefore there is a well made function 'put':
const { Pair, Unit, curry, objOf, compose, State, mapProps, prop, option } = require("crocks"); const { put, get } = State; //reset :: () -> State String ()
const reset = () => put('default');
console.log(
reset()
.execWith('OLD') // default
)
console.log(
put('new')
.runWith('OLD') //Pair( (), "new" )
)
[Functional Programming Moand] Update The State Of A State Monad (put)的更多相关文章
- [Functional Programming] Read and Transform Values from a State ADT’s State (get)
Many times we need to access and transform state, either in part or in full, to be used when calcula ...
- [Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined
For example we have a component, it needs to call 'react-redux' connect function. import { compose, ...
- [Functional Programming] Introduction to State, thinking in State
Recently, I am learning Working with ADT. Got some extra thought about State Monad. Basiclly how to ...
- [Functional Programming ADT] Initialize Redux Application State Using The State ADT
Not only will we need to give our initial state to a Redux store, we will also need to be able to re ...
- [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 ...
- [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)
Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...
- [Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)
While sometimes outside input can have influence on how a given stateful transaction transitions, th ...
- [Functional Programming] Using ComposeK for both get State and modify State
We have State like this: const state = { cards: [ { id: "green-square", color: "green ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
随机推荐
- .net中 Timer定时器
作者:feiying008 在开发一套视觉系统时,发现系统内存一直不断增加,直至系统内存爆满.一开始还以为是程序内存泄露,是图像操作算法写的有问题,但是,发现如果电机轴如果 不运行的状态下,每隔一秒进 ...
- 深入解析php中的foreach问题
本篇文章是对php中的foreach问题进行了详细的分析介绍,需要的朋友参考下 前言:php4中引入了foreach结构,这是一种遍历数组的简单方式.相比传统的for循环,foreach能够更加便 ...
- NGUI 简单的背包系统
1.首先在场景中创建格子,用来存放物体的 2.为每一个格子设置标签为Item,建议只做一个格子,然后创建预制体就可以了,然后为每一个格子附加Box Collider组件,要用于检测嘛, 3.接下来就是 ...
- Knockout.js(二):监控数组属性(Observables Arrays)
如果想发现并响应一个对象的变化,就应该使用监控属性(observables),如果想发现并响应一个集合的变化,就需要使用监控属性数组(observableArray).在很多情况下,它都非常有用,比如 ...
- WebService的网络协议
SOAP协议.XML-RPC以及Hessian 是WebService的主要传输(编码)协议 SOAP即简单对象访问协议(Simple Object Access Protocol),它是用于交换XM ...
- tomcat服务器上web项目日志存放位置
1.找到log日志的配置文件:log.xml,或者log.property,找到rollingFile标签,该标签的fileName属性就是更新的日志文件的存放位置.(相对于tomcat的bin目录)
- Codeforces 500 E. New Year Domino
\(>Codeforces \space 500 E. New Year Domino<\) 题目大意 : 数轴上有序排列着 \(n\) 块多米诺骨牌, 第 \(i\) 块骨牌坐标为 \( ...
- [BZOJ3224]普通平衡树(旋转treap,STL-vector)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 20328 Solved: 8979[Submit][St ...
- 20162312 实验一 Linux基础与Java开发环境
实 验 报 告 课程:程序设计与数据结构 姓名:张家铖 班级:1623 学号:20162312 实验名称: Linux基础与Java开发环境 实验器材:msi GL62M 7RD 实验目的与要求:1. ...
- linux中django部署
下载nginx yum install nginx rpm -ql nginx systemctl start nginx /usr/share/nginx/html # html页面 /etc/ng ...