[Functional Programming] 1. Function modelling -- Combine functions
Let's say we have two fucntions:
const toUpper = s => s.toUpperCase();
const exclaim = s => `${s}!`;
We want to combine those.
But function itself doesn't give us method to combine tow functions.
Therefore we can create a Monoid to combine function:
1 2 3 4 5 6 | const Fn = run => ({ run, concat(otherFn) { } }) |
So 'Fn' take a 'run' function as arguement and because we want to combine different function, we need to implement the interface for monoid 'concat'.
Inside 'concat' function, first, it should return 'Fn', so that we can compose later.
1 2 3 4 5 6 | const Fn = run => ({ run, concat(otherFn) { return Fn() } }) |
Becasue 'Fn' take a function, what we want is running the Fn with passed in data first then running other Fn with the same data.
1 2 3 4 5 6 | const Fn = run => ({ run, concat(otherFn) { return Fn(x => run(x).concat(otherFn.run(x))) } }) |
The highlighted 'concat' function is 'String.prototyp.concat', not the 'Fn.concat'. Because 'String' is semigroup, so we can do 'concat'.
Now, if we runt the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | const Fn = run => ({ run, concat(otherFn) { return Fn(x => run(x).concat(otherFn.run(x))) } }) const toUpper = s => s.toUpperCase(); const exclaim = s => `${s}!`; const res = Fn(toUpper) .concat(Fn(exclaim)) .run( "happy flow" ); // HAPPY FLOWhappy flow! |
Functor
We can also make 'Fn' as a Functor. All we need to do is add 'map' function to it.
1 2 3 4 5 6 7 8 | const Fn = run => ({ run, concat(otherFn) { return Fn(x => run(x).concat(otherFn.run(x))) }, map(f) { } }) |
Inside 'map'. We always need to return a 'Fn' in order to keep chaining the call:
1 2 3 4 5 6 7 8 9 | const Fn = run => ({ run, concat(otherFn) { return Fn(x => run(x).concat(otherFn.run(x))) }, map(f) { return Fn() } }) |
And 'Fn' take a 'function' as arguement to do trasformation.
map(f) { return Fn(f => f(...)) }
Inside 'f(...)', we need to get the value by calling 'run(x)':
1 2 3 4 5 6 7 8 9 | const Fn = run => ({ run, concat(otherFn) { return Fn(x => run(x).concat(otherFn.run(x))) }, map(f) { return Fn(f => f(run(x))) } }) |
Example:
const res = Fn(toUpper) .concat(Fn(exclaim)) .map(s => s.slice(5)) .run("happy flow"); // FLOWhappy flow!
【推荐】国内首个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工具
2019-03-13 [Algorithm] Universal Value Tree Problem
2019-03-13 [HTML5] Using the focus event to improve navigation accessibility (nextElementSibling)
2019-03-13 [Javascript] Coding interview problem: Scheduler functional way
2018-03-13 [HTML 5] Atomic Relevant Busy
2018-03-13 [HTML 5] aria-live
2018-03-13 [Node.js] Proxy Requests for Local and Remote Service Parity
2018-03-13 [Node.js] Manage Configuration Values with Environment Variables