[Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)
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)的更多相关文章
- [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers
Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Test Design Techniques - STATE BASED TESTING
Test Design Techniques - STATE BASED TESTING -Test note of “Essential Software Test Design” 2015-08- ...
随机推荐
- PHP会话控制
会话控制的思想是指能够在网站中根据一个会话跟踪用户. PHP的会话是通过唯一的会话ID来驱动的.会话ID是一个加密的随机数字.它由PHP生成,在会话的生命周期中都会保存在客户端.它 ...
- YYH的球盒游戏(NOIP模拟赛Round 6)
题目描述 YYH有一些总共有种颜色的球,他有颜色的球个.他同样有个盒子,第个盒子能放个球. 他的目标是把这个球按规则放进个盒子里: 对于一个盒子,对于每种颜色的球至多只能放个. 把颜色为的球放进盒子, ...
- CCAction详解
http://blog.csdn.net/bailongvip/article/details/7895370 转载自雨松MOMO程序研究院 上一章我们了解了CCNode的实现原理,这次我跟大家探讨一 ...
- 谈谈dpdk应用层包处理程序的多进程和多线程模型选择时的若干考虑
看到知乎上有个关于linux多进程.多线程的讨论:http://www.zhihu.com/question/19903801/answer/14842584 自己项目里也对这个问题有过很多探讨和测试 ...
- python--jinja2
from jinja2 import Template # 创建一个Template模板去渲染它 s = "my name is {{mashiro}}" t = Template ...
- Ubuntu16.04搭建LAMP开发环境
Ubuntu16.04搭建LAMP开发环境 虚拟机上安装好Ubuntu16.04后,是一台空白的Ubuntu.我的目的是搭建LAMP环境,顺便搭一个Python Django环境. 基本设置 1.配置 ...
- Codeforces538F A Heap of Heaps(函数式线段树)
题意:给你一个数组a[n],对于数组每次建立一个完全k叉树,对于每个节点,如果父节点的值比这个节点的值大,那么就是一个违规点,统计出1~n-1完全叉树下的违规点的各自的个数. 一个直觉的思想就是暴力, ...
- JDK7集合框架源码阅读(一) ArrayList
基于版本jdk1.7.0_80 java.util.ArrayList 代码如下 /* * Copyright (c) 1997, 2013, Oracle and/or its affiliates ...
- 浅谈如何做好Bug回归验证?
作为测试人员,我们都知道Bug的生命周期是: 我们都希望自己不仅有敏锐的洞察力能够全面的找出隐藏在软件中的bug,还希望自己有系统的分析能力能够准确的分析出每个bug的原因以至于能正确.全面的解决修复 ...
- 反汇编引擎Capstone
反汇编引擎Capstone Capstone是Kali Linux自带的一款轻量级反汇编引擎.它可以支持多种硬件构架,如ARM.ARM64.MIPS.X86.该框架使用C语言实现,但支持C++.P ...