While sometimes outside input can have influence on how a given stateful transaction transitions, there are many times where the current state at the time of a transaction. We can see the power of this type of transaction by seeing what it would take to read from two different locations in state in parallel and then pass on the result of combining them under a comparison operation to another transition that will set a different location based on the result.

const {prop, State, omit, converge,map, composeK, liftA2, equals, constant,option, chain, mapProps, find, propEq, isNumber, compose, safe} = require('crocks');
const {get, modify, of} = State; // getState :: String -> State Object (Maybe a)
const getState = key => get(prop(key)); // liftState :: ( a -> b) -> a -> State s b
const liftState = fn => compose(
of,
fn // getfn return value and pass into State.of
); // getHint :: () -> State AppState Hint
const getHint = () => getState('hint')
.map(option({color: 'yay', shape: 'uwu'})); // getCard :: String -> State AppState Card
const getCard = (id) => getState('cards')
.map(chain(find(propEq('id', id)))) // find() return a Maybe, so need to use chain to unfold the value
.map(option({id: null, color: 'unk', shape: 'unk'})); // cardToHint :: State AppState Hint
const cardToHint = composeK(
liftState(omit(['id'])),
getCard
) // setIsCorrect :: Boolean -> State AppState ()
const setIsCorrect = (b) => modify(mapProps({'isCorrect': constant(b)})); const validateAnswer = converge(
liftA2(equals),
cardToHint,
getHint
) const feedback = composeK(
setIsCorrect,
validateAnswer
) ///////////////////////////////////////////////////// const state = {
cards: [
{id: 'green-square', color: 'green', shape: 'square'},
{id: 'orange-square', color: 'orange', shape: 'square'},
{id: 'blue-triangle', color: 'blue', shape: 'triangle'}
],
hint: {
color: 'green',
shape: 'square'
},
isCorrect: null
} console.log(
feedback('green-square')
.execWith(state)
)

[Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)的更多相关文章

  1. [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers

    Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...

  2. [Functional Programming] Using JS, FP approach with Arrow or State Monad

    Using Naive JS: const {modify, get} = require('crocks/State'); const K = require('crocks/combinators ...

  3. [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 ...

  4. [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 ...

  5. [Functional Programming Monad] Modify The State Of A State Monad

    Using put to update our state for a given state transaction can make it difficult to modify a given ...

  6. [Functional Programming Monad] Refactor Stateful Code To Use A State Monad

    When we start to accumulate functions that all work on a given datatype, we end up creating a bunch ...

  7. [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 ...

  8. [Functional Programming] Combine State Dependent Transactions with the State ADT (composeK to replace multi chian call)

    When developing a Finite State Machine, it is often necessary to apply multiple transitions in tande ...

  9. Test Design Techniques - STATE BASED TESTING

    Test Design Techniques - STATE BASED TESTING -Test note of “Essential Software Test Design” 2015-08- ...

随机推荐

  1. ACdream 1029 前缀和

    Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Subm ...

  2. elemetUi 组件--el-checkbox

    [需求]实现选择右边的

  3. 汕头市队赛 SRM 09 B 撕书

    B 撕书II-3 SRM 09 背景&&描述 琉璃手头有一黑一白两本魔法书,一本是<缟玛瑙的不在证明>,另一本是<白色相簿1.5>     传说同时打开这两本书 ...

  4. [hdu1695] GCD ——欧拉函数+容斥原理

    题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...

  5. js实景题

    测天然气流量的仪器每5分钟向数据库插入一条数据,[{time:18:05,value:222},{time:18:10,value:333},{time:18:15,value:444}....... ...

  6. Android平台开发-WIFI 驱动移植 -- 详细

    一.WIFI的基本架构(代码路径)     1.WIFI Settings应用程序:       packages/apps/Settings/src/com/android/settings/wif ...

  7. python 666

    运行下面代码会输出什么? __builtins__.我们来运行下这行代码看看="666" #!/usr/bin/env python # encoding: utf-8 def _ ...

  8. Linux下用gSOAP开发Web Service服务端和客户端程序(一)

    1.功能说明: 要开发的Web Service功能非常简单,就是一个add函数,将两个参数相加,返回其和. 2.C版本的程序: (1)头文件:SmsWBS.h,注释部分不可少,url部分的IP必须填写 ...

  9. 将log4j2的配置文件log4j2.xml拆分成多个xml文件

    在日常的项目开发中,我们可能会使用log4j2分离系统日志与业务日志 ,这样一来,log4j2.xml 这个配置文件可能就会变得非常臃肿.庞大,那么我们可以将这个文件拆分成多个配置文件吗? 答案是肯定 ...

  10. hdu 1558(计算几何+并查集)

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...