[Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)
We take a closer look at the get
construction helper and see how we can use it to lift a function that maps the state portion and updates the resultant with the result. Using get
in this fashion, we then demonstrate how we can make accessors that can then be extended to create more complex interactions.
As there are times that we only want to pull the resultant for a given computation, we take a look at running our instances with the evalWith
method. evalWith
will run our computations, throwing away the state, returning the resultant.
get() function return a Pair(a, s). 'a' is the return value (variable) and 's' is the orginal state.
const { curry, compose, State, mapProps, prop, option } = require("crocks"); const { modify, get } = State; const burgers = { burgers: 4 } const taocs = { taocs: 10 } const res = get() .map(prop('burgers')) .runWith(burgers) console.log(res); // Pair( Just 4, { burgers: 4 } )
One thing we can improve from the code above is combine
// From get() .map(prop('burgers')) // To get(prop('burgers'))
Second the return value for the 'a' is wrapped in Maybe, we want to provided some default value for Nothing case:
const res = get(prop('taocs')) .runWith(burgers) console.log(res); // Pair( Nothing, { burgers: 4 } )
To do that we can create 'defaultProp':
const defaultProp = (key, def) => get(prop(key)).map(option(def)); const res = defaultProp('taocs', 0) .runWith(burgers) console.log(res); // Pair( 0, { burgers: 4 } )
Thrid, we can 'compose' to simply the code further:
const defaultProp = (key, def) => compose( option(def), prop(key) ) const getBurgers = get(defaultProp('taocs', 0)) const res = getBurgers .runWith(burgers)
Last, we don't really care about the Pair(0, {burgers: 4}), we only interest the value: we can replace 'runWith' with 'evalWith':
const defaultProp = (key, def) => compose( option(def), prop(key) ) const getBurgers = get(defaultProp('burgers', 0)) const res = getBurgers .evalWith(burgers) // 4
----
const { curry, objOf, compose, State, mapProps, prop, option } = require("crocks"); const { modify, get } = State; const burgers = { burgers: 4 } const taocs = { taocs: 10 } const defaultProp = (key, def) => compose( option(def), prop(key) ) const getBurgers = get(defaultProp('burgers', 0)) const burgerToTacos = getBurgers.map(objOf('tacos')); const res = burgerToTacos .evalWith(burgers) // 4 console.log(res); // { tacos: 4 }
【推荐】国内首个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工具
2018-01-29 [Javascript] String Padding in Javascript using padStart and padEnd functions
2016-01-29 [Redux] Extracting Container Components (FilterLink)
2016-01-29 [Regular Expressions] Introduction