在 ramda 中调用具有相同参数的函数列表
Call a list of functions with the same argument in ramda
假设我有一个函数列表,我想用相同的参数调用这些函数并得到一个结果列表。这是我的 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]]
我不明白为什么 x => x(input)
不等同于 call(__, input)
。我的想法是,call(__, input)
将 return 一个将使用 input
.
调用其第一个参数的函数
你能解释一下我做错了什么吗?我的直觉是我误解了 __
的用法。我该如何使用 call
或其他一些内置函数来优雅地编写它?
我也试过了,
// this also works and produces [4, 5, 6]
map(flip(call)(input), fns)
但由于风格原因,这也不适合我。我觉得我在用 flip
硬塞东西,而且我也不喜欢连续的函数调用 (...)(...)
.
您正在寻找R.juxt:
juxt applies a list of functions to a list of values.
const { add, juxt } = R
const input = 2
const fns = [add(2), add(3), add(4)]
const addTo = juxt(fns)
const result = addTo(input)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
Ori Drori 表明 juxt
会做你想做的事。
至于为什么 call
不起作用...老实说,这可能是所有 Ramda 中设计最差的功能。 (我是 Ramda 的创始人之一,我想它是我写的,所以除了我自己,我没有侮辱任何人。)
几乎不需要它。既然可以写 fn(1, 2)
,为什么还要写 call(fn, 1, 2)
。柯里化不能正常工作,因为它是可变的,这本身就是一个问题。甚至这个例子也很不清楚,主要是因为很难找到任何有意义的例子。
我个人会跳过使用它。
But I don't quite like the use of the arrow function (purely for stylistic reasons), so I wanted to rewrite it as,
从评论中,我假设您不是指与函数声明或函数表达式相反的箭头函数,而是您更愿意无意义地编写此代码。一个建议:使用无点样式,它可以使您的代码更具可读性,但如果没有则跳过它;不要迷恋它。
此处 juxt([add(2), add(3), add(4)])
似乎不错,只要 juxt
(juxtapose
的缩写)对您有意义即可。但很多时候,推动免积分似乎弊大于利。
I also tried,
// 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
,
我倾向于同意。 flip
几乎不是解决任何问题的好方法。但我通常更不喜欢占位符。就是没有让我满意!
and I also don't like the consecutive function calls (...)(...)
.
这里我不同意。我已将自己的代码切换为几乎通用的完全柯里化风格。如果这涉及多个连续的函数调用和可怕的括号对接,那就这样吧。我只是发现它更容易使用和推理。
假设我有一个函数列表,我想用相同的参数调用这些函数并得到一个结果列表。这是我的 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]]
我不明白为什么 x => x(input)
不等同于 call(__, input)
。我的想法是,call(__, input)
将 return 一个将使用 input
.
你能解释一下我做错了什么吗?我的直觉是我误解了 __
的用法。我该如何使用 call
或其他一些内置函数来优雅地编写它?
我也试过了,
// this also works and produces [4, 5, 6]
map(flip(call)(input), fns)
但由于风格原因,这也不适合我。我觉得我在用 flip
硬塞东西,而且我也不喜欢连续的函数调用 (...)(...)
.
您正在寻找R.juxt:
juxt applies a list of functions to a list of values.
const { add, juxt } = R
const input = 2
const fns = [add(2), add(3), add(4)]
const addTo = juxt(fns)
const result = addTo(input)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
Ori Drori 表明 juxt
会做你想做的事。
至于为什么 call
不起作用...老实说,这可能是所有 Ramda 中设计最差的功能。 (我是 Ramda 的创始人之一,我想它是我写的,所以除了我自己,我没有侮辱任何人。)
几乎不需要它。既然可以写 fn(1, 2)
,为什么还要写 call(fn, 1, 2)
。柯里化不能正常工作,因为它是可变的,这本身就是一个问题。甚至这个例子也很不清楚,主要是因为很难找到任何有意义的例子。
我个人会跳过使用它。
But I don't quite like the use of the arrow function (purely for stylistic reasons), so I wanted to rewrite it as,
从评论中,我假设您不是指与函数声明或函数表达式相反的箭头函数,而是您更愿意无意义地编写此代码。一个建议:使用无点样式,它可以使您的代码更具可读性,但如果没有则跳过它;不要迷恋它。
此处 juxt([add(2), add(3), add(4)])
似乎不错,只要 juxt
(juxtapose
的缩写)对您有意义即可。但很多时候,推动免积分似乎弊大于利。
I also tried,
// 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
,
我倾向于同意。 flip
几乎不是解决任何问题的好方法。但我通常更不喜欢占位符。就是没有让我满意!
and I also don't like the consecutive function calls
(...)(...)
.
这里我不同意。我已将自己的代码切换为几乎通用的完全柯里化风格。如果这涉及多个连续的函数调用和可怕的括号对接,那就这样吧。我只是发现它更容易使用和推理。