[Functional Programming Monad] Combine Stateful Computations Using A State Monad
Using State Programming functional Monad Combine
2023-09-14 09:00:49 时间
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) )
相关文章
- [Postman & Integration Tests] Using Postman to Define Integration Tests
- [Angular] Using Pipe for function memoization
- [Functional Programming] Using JS, FP approach with Arrow or State Monad
- [Functional Programming Monad] Combine Stateful Computations Using A State Monad
- [Functional Programming] Define Discrete State Transitions using the State ADT
- [Angular] Expose Angular Component Logic Using State Reducers
- [Angular] Two ways to create Angular Animation, using animation() or using state()
- [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
- [Angular 2] Using ng-model for two-way binding
- mysql全局权限账户%登录不上ERROR 1045 (28000): Access denied for user 'mhz'@'localhost' (using password: YES)
- [React Testing] Debug the DOM State During Tests using React Testing Library’s debug Function
- [Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)
- [Algorithms] Quicksort algorithm using TypeScript
- [MST] Describe Your Application Domain Using mobx-state-tree(MST) Models
- [Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe
- [CSS] Reduce Ambiguity in Class Names using a Naming Convention
- [AngularFire2] Build a Custom Node Backend Using Firebase Queue
- [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
- Error:全局变量不明白(using namespace std 与全局变量的冲突)
- (6)C#中using(...){...}的意思