I'm new to lodash and just playing around with it to become familiar. I'm trying to curry a flipped function and I'm getting a 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'. 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
2
> _.flip(_.map).length
0
_.curry
lets you specify the number of parameters to work around that:
const curriedFlippedMap = _.curry(flippedMap, 2);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.