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. poj 3648 Wedding 2-SAT问题入门题目

    Description Up to thirty couples will attend a wedding feast, at which they will be seated on either ...

  2. Java中Collections的binarySearch方法

    方法一 public static <T> int binarySearch(List<? extends Comparable<? super T>> list, ...

  3. 《Linux命令、编辑器与shell编程》第三版 学习笔记---001

    Linux概述 1.具有内核编程接口 2.支持多用户(同时) 3.支持多任务 4.支持安全的分层文件系统 a.标准 b.链接 c.权限 5.shell(命令解释器和编程语言) a.文件名生成(通配符和 ...

  4. Centos 6.3软件安装

    一.软件安装包的类型: 1. tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2. rpm包,如software-1.2.3-1.i386.r ...

  5. [译]java9新特性:在接口中用pirvate方法让default(java8接口特性)更简练

    Java8 带来了许多改变,其中之一就是default修饰的接口方法. 这些方法改变了我们已知的接口,现在我们能够在接口中定义默认实现方法.默认实现方法的不同之处在于,在接口中用default修饰抽象 ...

  6. spring JPA写法一种

    第一次用,搞了半天,终于知道了大概. 基于ORM的JPA还是蛮好用的, 这次是实现一个MANGODB的日志存储和检索. PRISM用的. repository的写法: package paic.sto ...

  7. [loj#101] 最大流 网络流模板

    #101. 最大流 内存限制:512 MiB时间限制:5000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 这是一道模板题. 给定  ...

  8. AC日记——Little Elephant and Numbers codeforces 221b

    221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...

  9. 将C#程序做成服务后服务自动停止的问题

    查了好几天,没法调试实在是很难找错误,今天想了半天到事件查看器,提示如下: 说明: 由于未经处理的异常,进程终止. 异常信息: System.NullReferenceException 想了半天,应 ...

  10. "科林明伦杯"哈尔滨理工大学第八届程序设计竞赛 题解

    题目链接  Problems Problem A 快速幂累加即可. #include <cstdio> #include <cstring> #include <iost ...