简体   繁体   English

使用lodash调用翻转函数-有限制吗?

[英]Currying a flipped function using lodash - Are there limitations?

I'm new to lodash and just playing around with it to become familiar. 我是lodash的新手,只是玩弄它变得熟悉。 I'm trying to curry a flipped function and I'm getting a TypeError. 我试图咖喱一个翻转的功能,我得到一个TypeError。

Currying the same 'unflipped' function works as expected. 使用相同的“未翻转”功能可以正常工作。

const curriedMap = _.curry(_.map);
const squares1 = curriedMap([ 1, 2, 3, 4 ]);

console.log(squares1(x => x * x)); // [ 1, 4, 9, 16 ]


const flippedMap = _.flip(_.map);

console.log(flippedMap(x => x * x, [1, 2, 3, 4])); // [ 1, 4, 9, 16 ]

const curriedFlippedMap = _.curry(flippedMap);

const makeSquares = curriedFlippedMap(x => x * x);

console.log(makeSquares([1, 2, 3, 4])); // TypeError: makeSquares is not a function

I'm expecting the last line to produce [ 1, 4, 9, 16 ] , but instead I get 'TypeError'. 我期待最后一行产生[ 1, 4, 9, 16 ] 1,4,9,16 [ 1, 4, 9, 16 ] ,但是我得到'TypeError'。 What am I doing wrong? 我究竟做错了什么?

_.map has a length property (number of parameters) that _.curry can use to curry it automatically, but _.flip(_.map) can't easily produce a new function with the same length as its input (it reverses the entire argument list, it's not just f => (a, b) => f(b, a) ). _.map具有length属性(参数数量), _.flip(_.map) curry可以使用该_.curry自动对其进行咖喱处理,但是_.flip(_.map)不能轻易产生与输入长度相同的新函数(相反整个参数列表,而不仅仅是f => (a, b) => f(b, a) )。

> _.map.length
2

> _.flip(_.map).length
0

_.curry lets you specify the number of parameters to work around that: _.curry可让您指定要解决的参数数量

const curriedFlippedMap = _.curry(flippedMap, 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM