[Function Programming] Function modelling -- 9. Monad Transformers
-- Function Programming Transformers Monad
2023-09-14 08:59:13 时间
Path: Compose Functors -> Monad Transformers -> Free Monad
Let's first see how much it sucks when dealing with nested Monads (without natural transformation).
const { TaskT, Task, Either } = require("../types"); const _ = require("lodash"); const users = [ { id: 1, name: "Brian" }, { id: 2, name: "Marc" }, { id: 3, name: "Odette" }, ]; const following = [ { user_id: 1, follow_id: 3 }, { user_id: 1, follow_id: 2 }, { user_id: 2, follow_id: 1 }, ]; const find = (table, query) => Task.of(Either.fromNullable(_.find(table, query))); const app = () => find(users, { id: 1 }) // Task(Either(User)) .chain((eitherUser) => eitherUser.fold(Task.rejected, (user) => find(following, { follow_id: user.id }) ) ) .chain((eitherUser) => eitherUser.fold(Task.rejected, (foUser) => find(users, { id: foUser.user_id }) ) ) .fork(console.error, (eu) => eu.fold(console.error, console.log)); app(); // { id: 2, name: 'Marc' }
As you can see, the code is trying to find your follower's follower.
Each time we need to .chain() + .fold()...
Then .fork() + .fold()...
Which is confusing.
To solve the problem, we can include Monad transform:
const TaskEither = TaskT(Either);
Redefine 'find' function:
const find = (table, query) =>
TaskEither.lift(Either.fromNullable(_.find(table, query)));
Difference between .of() vs .lift():
// TaskEither.of(Either) --> Task(Either(Either)) // TaskEither.lift(Either) --> Task(Either)
Then we can simpify our app:
const app = () => find(users, { id: 1 }) // Task(Either(User)) .chain((user) => find(following, { follow_id: user.id })) .chain((foUser) => find(users, { id: foUser.user_id })) .fork(console.error, (eu) => eu.fold(console.log, console.log)); app();
--
Full ocde:
const { TaskT, Task, Either } = require("../types"); const _ = require("lodash"); const TaskEither = TaskT(Either); const users = [ { id: 1, name: "Brian" }, { id: 2, name: "Marc" }, { id: 3, name: "Odette" }, ]; const following = [ { user_id: 1, follow_id: 3 }, { user_id: 1, follow_id: 2 }, { user_id: 2, follow_id: 1 }, ]; const find = (table, query) => TaskEither.lift(Either.fromNullable(_.find(table, query))); const app = () => find(users, { id: 1 }) // Task(Either(User)) .chain((user) => find(following, { follow_id: user.id })) .chain((foUser) => find(users, { id: foUser.user_id })) .fork(console.error, (eu) => eu.fold(console.log, console.log)); app();
相关文章
- 程序员的量化交易之路(39)--Lean之BacktestingRealTimeHandler回测实时时间事件处理7
- [Javascript] Broadcaster + Operator + Listener pattern -- 10. Define a Function to Set Common Behaviors in Operators
- [Functional Programming] Function modelling -- 10. Free Monads
- [Functional Programming] Function modelling -- 8. Compose Functors
- [Functional Programming] Function modelling -- 6. contramap
- [Functional Programming] Function modelling -- 5. Endo Monoid
- 解决掉你心中 js function与Function的关系的疑问
- 2-scala文件操作--自动关闭打开的资源,读取properties文件
- centos笔记1--安装服务后启动不识别
- 《android开发艺术探索》读书笔记(四)--View工作原理
- [Functional Programming] 2. Function modelling -- Validation
- 动画--过渡函数 transition-timing-function
- xxx.forEach is not a function(DOM集合--类数组对象转化为数组)
- 【云原生 | Kubernetes 系列】--Envoy高级路由配置
- atitit.自己动手开发编译器and解释器(2) ------语法分析,语义分析,代码生成--attilax总结
- Qt资源应用--菜单图标
- Python项目学习二:项目2:绘制图表--初次实现
- 【Android Gradle 插件】AaptOptions 配置 ② ( additionalParameters 附加参数配置 | --rename-manifest-package 配置 )
- guava学习--Function、Predicate
- 记得12306货运系统“抢购空”编写插件--chrome交互式插件的各个部分
- 2020电赛E题--非线性失真器程序设计-02-使用STM32实现THD高精度测量采集(附整个工程gitee链接)
- 【讲座笔记】深度学习在通信领域的应用--Byonghyo Shim教授
- 【云原生 | Kubernetes 系列】--Envoy分布式跟踪