首页 > 代码库 > [Compose] 21. Apply Natural Transformations in everyday work
[Compose] 21. Apply Natural Transformations in everyday work
We see three varied examples of where natural transformations come in handy.
const Right = x => ({ chain : f => f(x), ap : other => other.map(x), traverse : (of, f) => f(x).map(Right), map : f => Right(f(x)), fold : (f, g) => g(x), concat : o => o.fold(_ => Right(x), y => Right(x.concat(y))), toString : () => `Right(${x})`});const Left = x => ({ chain : f => Left(x), ap : other => Left(x), traverse : (of, f) => of(Left(x)), map : f => Left(x), fold : (f, g) => f(x), concat : o => o.fold(_ => Left(x), y => o), toString : () => `Left(${x})`});const fromNullable = x => x != null ? Right(x) : Left(null);const Task = require(‘data.task‘);const {List} = require(‘immutable-ext‘);const fake = id => ({ id, name: ‘user1‘, best_friend_id: id + 1 });const Db = ({ find: id => new Task((rej, res) => { res(id > 2 ? Right(fake(id)) : Left(‘not found‘)) })});const eitherToTask = e => e.fold( Task.rejected, Task.of ); // Right(x) --> Task(user)const res = Db.find(3) // Task(Right(user)) .chain(eitherToTask) // Task(user) .chain(user => Db.find(user.best_friend_id) ) // Task(Right(user)) .chain(eitherToTask) // Task(user) .fork(e => console.error(e), r => console.log(r) );
[Compose] 21. Apply Natural Transformations in everyday work
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。