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. 配置postfix支持虚拟域和虚拟用户

    请先看基础篇  https://www.cnblogs.com/hellojackyleon/p/9281620.html https://sourceforge.net/projects/couri ...

  2. webpack最佳入门实践系列(6)

    10.css模块化 10.1.什么是css模块? CSS模块就是所有的类名都只有局部作用域的CSS文件,当你在一个JavaScript模块中导入一个CSS文件时,CSS模块将会定义一个对象,将文件中类 ...

  3. Bzoj2165 大楼

    Time Limit: 40 Sec  Memory Limit: 259 MBSubmit: 779  Solved: 285[Submit][Status][Discuss] Descriptio ...

  4. OpenGL入门学习(五)

    http://developer.178.com/201103/94955548786.html 今天要讲的是三维变换的内容,课程比较枯燥.主要是因为很多函数在单独使用时都不好描述其效果,我只好在最后 ...

  5. UVA 306 Cipher

    题意 :lucky cat里有翻译.英文也比较好懂. 很容易发现有周期然后就拍就好了 注意每组数据后边都有空行 包括最后一组.一开始以为最后一组没有空行.唉.. #include <map> ...

  6. VS2005调试无法进入断点,暂停、停止、重编按钮为灰色

    没装IE8之前(IE7),Vs2005调试还好用,装了IE8之后, Vs2005的WebSite程序按F5无法正常调试. 需要手动附加到进程, 比较麻烦, 据说还有种方法, 把所有IE窗口都关掉, 再 ...

  7. OutputDebugString方便格式化WIN32封装

    void TRACE(LPCTSTR lpszFmt, ...) { va_list args; va_start(args, lpszFmt); ; TCHAR *lpszBuf = (TCHAR* ...

  8. [BZOJ1052][HAOI2007]覆盖问题 二分+贪心

    1052: [HAOI2007]覆盖问题 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2053  Solved: 959 [Submit][Sta ...

  9. 模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合)

    模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合) Code: #include <bits/stdc++.h> using namespace std; #define ...

  10. Longest Common Substring($LCS$)

    Longest Common Substring(\(LCS\)) 什么是子序列? 子序列就是某一个序列的不连续的一部分. 如图, \(abcde\)就是图中序列的一个子序列. 公共子序列 公共子序列 ...