[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 ...
随机推荐
- Linux下使进程在后台运行
怎么样使程序在后台执行 /////////////////// nohup ./nn > nn.log 2 > &1 & //////////// 方法有很多, ...
- 洛谷P2520向量
题目传送门 看到数据范围其实就可以确定这是一道结论题. 首先分析,给定你的向量的两个坐标a,b有八种组合方式可以用,但实际上整理一下可以得出实际上只有五种,x/y ±2a,x/y ±2b,x+a,y+ ...
- 树形dp入门(poj 2342 Anniversary party)
题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚 ...
- [LOJ6191][CodeM]配对游戏(概率期望DP)
n次向一个栈中加入0或1中随机1个,如果一次加入0时栈顶元素为1,则将这两个元素弹栈.问最终栈中元素个数的期望是多少. 首先容易想到用概率算期望,p[i][j][k]表示已加入i个数,1有j个,总长为 ...
- poj 1681(Gauss 消元)
Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5875 Accepted: 2825 ...
- AGC 018 A - Getting Difference
题面在这里! 天呐,我已经做了一天水题了mmp 养生最重要,恩. 首先发现最终序列里的元素肯定是 <= max 的,因为无论何时序列里都不会有负数,所以减的话不会变大(反向大只有>2*ma ...
- [Codeforces-div.1 24D] Broken robots
[Codeforces-div.1 24D] Broken robots 试题分析 显然设\(f_{i,j}\)为到\((i,j)\)的期望步数,将转移表达式列出来. 首先自己跟自己的项消掉. 然后规 ...
- bzoj 4428: [Nwerc2015]Debugging调试
4428: [Nwerc2015]Debugging调试 Description Your fancy debugger will not help you in this matter. There ...
- 浅析HashMap和Hashtable的区别
HashMap和Hashtable两个类都实现了Map接口,二者保存键值对(key-value对): HashMap和HashTable区别 第一,继承的父类不同.HashMap继承自Abstract ...
- bootstrap学习(全局CSS样式)(二)
标题类:.h1到.h6 页面主体 bootstrap将全局font-size设置为14px,line-height设置为1.428,这些属性 直接赋予元素和所有段落元素. 文本对齐类 text-lef ...