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. 3.2 Lucene实战:一个简单的小程序

    在讲解Lucene索引和检索的原理之前,我们先来实战Lucene:一个简单的小程序! 一.索引小程序 首先,new一个java project,名字叫做LuceneIndex. 然后,在project ...

  2. 用Java画QRCode二维码

    支付宝.微信扫码支付的二维码,第三方的类库QRCode.jar 还是很好用的.下面贴出来这个东东生成二维码的代码. 使用时注意包括图片地址.编码内容.图片属性等几个参数,支付宝的它们的扫码回调地址. ...

  3. codeforce 570 problem E&& 51Nod-1503-猪和回文

    1503 猪和回文 一只猪走进了一个森林.很凑巧的是,这个森林的形状是长方形的,有n行,m列组成.我们把这个长方形的行从上到下标记为1到n,列从左到右标记为1到m.处于第r行第c列的格子用(r,c)表 ...

  4. MFC does not support WINVER less than 0x0501 解决方案(转)

    原文转自 http://blog.csdn.net/ygzhong000/article/details/41750841 解决方案:在stdafx.h头文件中添加以下行. #define WINVE ...

  5. MFC 实现打印机打印功能

    Visual C++6.0是开发Windows应用程序的强大工具,但是要通过它实现程序的打印功能,一直是初学者的一个难点,经常有朋友询问如何在VC中实现打印功能,他们往往感到在MFC提供的框架内实现这 ...

  6. java基础练习 17

    import java.util.Scanner; public class Seventheen { /*企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元 ...

  7. 去掉VS中的警告错误:warning C4819

    当项目引用到外部源代码后,经常出现4819错误,警告信息如下: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符.请将该文件保存为 Unicode 格式以防止数据丢失. ...

  8. DotNetCore 笔记

    最近公司框架升级到DotNetCore了,但是在用post请求的时候,发现页面的post data后台并没有接收到数据,经过调查发现,netcore用法和之前的MVC5不一样,想要接收post里的da ...

  9. 打印控件Lodop的使用

    前些天发现一个不错的打印的控件Lodop,下面就来介绍一下具体使用! 首先到官网:http://www.lodop.net/download.html 下载最新版,文档的话官网中有很详细的介绍,这里演 ...

  10. unittest (python标准库-开发工具-单元测试框架)

    unittest官方文档摘录 翻译 reffer to: https://docs.python.org/3/library/unittest.html#unittest.TextTestRunner ...