[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 ) )*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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