[Function Programming] Function modelling -- 9. Monad Transformers
-- Function Programming Transformers Monad
2023-09-14 09:00:47 时间
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();
相关文章
- Java IO--数据操作流DataOutputStream/DataInputStream
- Java泛型--泛型应用--泛型接口、泛型方法、泛型数组、泛型嵌套
- 【华为云技术分享】我眼中的华为公有云AI平台--ModelArts
- Lintcode--003(乱序字符串)
- jQuery插件开发--(转)
- java知识点查漏补缺-- 2020512
- 编写高质量代码:改善Java程序的151个建议 --[65~78]
- [Javascript] Broadcaster + Operator + Listener pattern -- 8. Pass a Done Symbol when an Async Function is Done
- [Function Programming] Function modelling -- 9. Monad Transformers
- [Functional Programming] Function modelling -- 6. contramap
- [Functional Programming] Function modelling -- 4. Reader Monda example
- [Functional Programming] Function modelling -- 3. Reader Monad
- [Functional Programming] 1. Function modelling -- Combine functions
- 动画--过渡函数 transition-timing-function
- Centos7安装部署openstack--Networking 网络服务(计算节点)
- sql基础--怎样查询所有列数据
- Spring Cloud -- Ribbon负载均衡
- [Intro to Deep Learning with PyTorch -- L2 -- N15] Softmax function
- [Intro to Deep Learning with PyTorch -- L2 -- N14] Sigmoid function
- [Functional Programming] Function modelling -- 7. contramap & Endo execrises
- [Functional Programming] Function modelling -- 4. Reader Monda example
- [Functional Programming] Function modelling -- 3. Reader Monad
- [Functional Programming] 1. Function modelling -- Combine functions
- 代码生成技术--CodeDom VS T4
- 动画--过渡函数 transition-timing-function
- xxx.forEach is not a function(DOM集合--类数组对象转化为数组)
- TOSCA自动化测试工具--建立测试用例
- k3.第二章 基于二进制包安装kubernetes v1.20 -- 集群部署(一)
- y91.第六章 微服务、服务网格及Envoy实战 -- 服务网格基础(二)
- L63.linux命令每日一练 -- 第九章 Linux进程管理命令 -- runlevel、init和service
- 5.2 calendar--通用日期的相关函数(3)
- matplotlib--animation
- 【MySql】MySQL数据库--什么是MySQL的回表 ?
- xxx.forEach is not a function(DOM集合--类数组对象转化为数组)