[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 tandem. To accomplish this in most Redux work flows requires at best, implementing multiple action handlers in separate reducers; or at worse, the need to duplicate logic for similar action handlers, sometime across multiple files. However when using a State ADT, we can easily combine these independent transitions by simply chain-ing multiple discrete transitions into one transaction. We demonstrate this interface by transitioning two portions of related state with one transaction.

 

复制代码
const { curry, compose, State, mapProps, composeK } = require("crocks");

const { modify } = State;

const state = {
  left: 8,
  moves: 0
};

const inc = x => x + 1;
const dec = x => x - 1;

const clamp = (min, max) => x => Math.min(Math.max(min, x), max);
const clampAfter = curry((min, max, fn) =>
  compose(
    clamp(min, max),
    fn
  )
);
const over = (key, fn) => modify(mapProps({ [key]: fn }));

const limitMoves = clampAfter(0, 8);

const decLeft = () => over("left", limitMoves(dec));
const incMoves = () => over("moves", limitMoves(inc));

// Then there are a series of chain functions, using composeK
/**
 * replace: 
 *  decLeft()
 *      .chain(decLeft)
 *      .chain(decLeft)
 *      .chain(decLeft)
 *      .chain(incMoves)
 *      .chain(incMoves)
 */
const applyMove = composeK(
    incMoves, incMoves, decLeft, decLeft, decLeft
)

const res = applyMove()
  .execWith(state);
console.log(res); //{ left: 5, moves: 2 }
复制代码

 

 

Another example:

复制代码
const state = {
    cards: [
        {id: 'green-square', color: 'green', shape: 'square'},
        {id: 'orange-square', color: 'orange', shape: 'square'},
        {id: 'blue-square', color: 'blue', shape: 'triangle'}
    ],
    left: 8,
    moves: 0
}

const {State, when, assign, map, mapProps, propEq} = require('crocks');
const {modify} = State;

const markSelected = id => assignBy(propEq('id', id), {selected: true})
const assignBy = (pred, obj) => when(pred, assign(obj));
const over = (key, fn) => modify(mapProps({ [key]: fn }));

const selectCard = id => over('cards', map(markSelected(id)))



console.log(
    JSON.stringify(
        selectCard('green-square').execWith(state),
        null,
        2
    )
);


/*
// Using Ramda to implememnt the same logic
const {compose, map, propOr, when, propEq, mergeLeft} = require('ramda');

const markAsSelected = (id) => when(propEq('id', id), mergeLeft({selected: true}))
const over = (key, fn) => compose(map(fn), propOr([], key));

const selectCard2 = (id) => over('cards', markAsSelected(id))

console.log(
    JSON.stringify(
        selectCard2('green-square')(state),
        null,
        2
    )
)*/
复制代码

 

posted @   Zhentiw  阅读(267)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-01-07 [React Testing] Children with Shallow Rendering
2016-01-07 [React Testing] Reusing test boilerplate
2016-01-07 [React Testing] Conditional className with Shallow Rendering
2016-01-07 [React Testing] className with Shallow Rendering
2015-01-07 [MODx] 9. Real Example
2015-01-07 [MODx] 8. Snippet get data, chunk display
2015-01-07 [MODx] 7. MIGX DB
点击右上角即可分享
微信分享提示