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

原文地址:https://www.cnblogs.com/Answer1215/p/10257131.html

时间: 2024-10-10 01:05:47

[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 in on specific attributes on our state, making it incompatible with using the State ADT. We would like a way to avoid keeping all of our reducers in

[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 reset our state at any time by dispatching an action. We can get the best of both worlds by having a function that will return an object with all of our

Test Design Techniques - STATE BASED TESTING

Test Design Techniques - STATE BASED TESTING -Test note of “Essential Software Test Design” 2015-08-19 Content: 13.1 The Model  13.1.1 The ATM Machine13.2 Creating Base Test Cases  13.2.1 Ways of Covering the Graph  13.2.2 Coverage According to Chow 

Apply Functional Programming Principles

Apply Functional Programming Principles Edward Garson FUNCTiONAL PROGRAMMiNG has recently enjoyed renewed interest from the mainstream programming community. Part of the reason is because emergent properties of the functional paradigm are well positi

Beginning Scala study note(4) Functional Programming in Scala

1. Functional programming treats computation as the evaluation of mathematical and avoids state and mutable data. Scala encourages an expression-oriented programming(EOP) 1) In expression-oriented programming every statement is an expression. A state

Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accepts 2 parameters, a function f :: T -> R and a list list :: [T]. [T] is a generic type paramterized by T, it's not the same as T, but definitely shares s

PROFESSIONAL FUNCTIONAL PROGRAMMING IN C# 学习

CHAPTER1 A Look at Functional Programming History CHAPTER2 Putting Functional Programming into a Modern Context CHAPTER3 Functions,Delegates,and Lambda Expressions CHAPTER4 Flexible Typing with Generics CHAPTER5 Lazy Listing with Iterators CHAPTER6 E

Functional Programming.

I have been seeing Redux for sometime, but recently I come to realize that , it's part of so called functional programming. The basic idea for functional programming is so simple, Declare all dependency as function input, no hidden input ( we should

Functional Programming in Javascript 中文翻译 —— 目录和介绍

原著:[美] Dan Mantyla 目录 前言1 Javascript函数式编程的力量--举个例子2 函数式编程基础3 建立函数式编程环境4 在Javascript中实现函数式编程的技术5 类型理论6 高级主题以及Javascript的缺陷7 Javascript中的函数式和面型对象编程8 Javascript中的函数式和面型对象编程 关于翻译的这本书 现在市面上有两本专注于javascript函数式编程的书,一本是<Functional Javascript>(下文简称FJS), 另一本就