[Functional Programming Monad] Combine Stateful Computations Using A State Monad
Using State Programming functional Monad Combine
2023-09-14 08:59:15 时间
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(1); // 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(10) .runWith(2) )
相关文章
- 【论文阅读】Detecting Spacecraft Anomalies Using LSTMs and Nonparametric Dynamic Thresholding
- 【错误记录】Gradle 配置报错 ( Using different JDK locations on different processes might cause Gradle to spa )
- ORA-23389: obsolete procedure; drop objects and recreate using new master ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-010661; Symbol: ER_NDB_BINLOG_USING_SERVER_ID_0_SLAVES_WILL_NOT; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-010791; Symbol: ER_NDB_USING_NODEID; SQLSTATE: HY000 报错 故障修复 远程处理
- Exploring the Benefits of Using htonl Function in Linux Networking(linuxhtonl)
- Exploring the Benefits of Using Linux and OTF for Streamlined Software Development.(linuxotf)