首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RE:在ramda-fantasy map方法中遇到错误

RE:在ramda-fantasy map方法中遇到错误
EN

Stack Overflow用户
提问于 2017-03-22 10:55:58
回答 1查看 34关注 0票数 0

我正在尝试理解FP中的map是如何工作的。我想测试一下map在函数式编程中是如何工作的。

下面是测试代码。

代码语言:javascript
复制
var R = require('ramda');
var M = require('ramda-fantasy').Maybe;
var Just = M.Just;
var Nothing = M.Nothing;
var safeDiv = R.curry(function (n, d) { return d === 0 ? Nothing() : Just(n / d); });
var lookup = R.curry(function (k, obj) { return k in obj ? Just(obj[k]) : Nothing(); });
var a2 = M.maybe(100, R.inc);
var a4 = M.of(10);
console.log(a2); //output [Function]
console.log(a4); //output Just{ value: 10}
console.log(a2(Nothing())); //output 100
console.log(a2(M.of(20))); //out 21
// This is why I can't understand
// I think it is the same M.of(20).map(a2) === a2(M.of(20)).
console.log(M.of(20).map(a2));
// Produce the followng error message
// /mnt/e/work/fpjs/node_modules/ramda-fantasy/src/Maybe.js:49
// return m.reduce(function(_, x) {
//     TypeError: m.reduce is not a function
//     at /mnt/e/work/fpjs/node_modules/ramda-fantasy/src/Maybe.js:49:12
//     at /mnt/e/work/fpjs/node_modules/ramda/src/internal/_curryN.js:37:27
//     at /mnt/e/work/fpjs/node_modules/ramda/src/internal/_arity.js:5:45
//     at Just.map (/mnt/e/work/fpjs/node_modules/ramda-fantasy/src/Maybe.js:56:18)
//     at Object.<anonymous> (/mnt/e/work/fpjs/data/test3.js:14:22)
//     at Module._compile (module.js:571:32)
//     at Object.Module._extensions..js (module.js:580:10)
//     at Module.load (module.js:488:32)
//     at tryModuleLoad (module.js:447:12)
//     at Function.Module._load (module.js:439:3) 
EN

回答 1

Stack Overflow用户

发布于 2017-03-22 11:00:34

我想它是同一个M.of(20).map(a2) === a2(M.of(20))

嗯,不,不是。当您在数据结构上对一个函数执行map时,将使用包含的值来调用它-在本例中为20。然后a2(20)抛出你得到的错误。

例如,您可以做的是M.of(20).map(R.inc) -结果将是Just {value: 21},而M.Nothing().map(R.inc)将生成Nothing

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42941872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档