Suppose I have a list of functions that I want to call with the same argument and get a list of results. Here's my setup:
let input = 2
let fns = [add(2), add(3), add(4)]
map(x => x(input), fns)
// this is what I want. Outputs [4, 5, 6]
但我不太喜欢使用箭头功能(纯粹出于风格原因),因此我想将其重写为:
map(call(__, input), fns)
// this doesn't work and produces [[Function],[Function],[Function]]
I can't figure out why x => x(input)
isn't equivalent to call(__, input)
. My thinking is, call(__, input)
will return a function that will call its first argument with input
.
Can you explain what I'm doing wrong? My hunch is that I have misunderstood the use of __
. And how do I use call
or some other built-in function to elegantly write this?
我也尝试过
// this also works and produces [4, 5, 6]
map(flip(call)(input), fns)
But this also does not sit well with me due to stylistic reasons. I feel like I am shoehorning something with flip
, and I also don't like the consecutive function calls (...)(...)
.