企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 练习 ``` // 练习 1 // ========== // 给定一个 user,使用 safeProp 和 map/join 或 chain 安全地获取 sreet 的 name var safeProp = _.curry(function (x, o) { return Maybe.of(o[x]); }); var user = { id: 2, name: "albert", address: { street: { number: 22, name: 'Walnut St' } } }; var ex1 = _.compose(chain(safeProp('name')), chain(safeProp('street')), safeProp('address')); // 练习 2 // ========== // 使用 getFile 获取文件名并删除目录,所以返回值仅仅是文件,然后以纯的方式打印文件 var getFile = function() { return new IO(function(){ return __filename; }); } var pureLog = function(x) { return new IO(function(){ console.log(x); return 'logged ' + x; }); } var ex2 = _.compose(chain(_.compose(pureLog, _.last, split('/'))), getFile); // 练习 3 // ========== // 使用 getPost() 然后以 post 的 id 调用 getComments() var getPost = function(i) { return new Task(function (rej, res) { setTimeout(function () { res({ id: i, title: 'Love them tasks' }); }, 300); }); } var getComments = function(i) { return new Task(function (rej, res) { setTimeout(function () { res([ {post_id: i, body: "This book should be illegal"}, {post_id: i, body: "Monads are like smelly shallots"} ]); }, 300); }); } var ex3 = _.compose(chain(_.compose(getComments, _.prop('id'))), getPost); ```