将函数列表应用于Ramda中的值 [英] Apply a list of functions to a value in Ramda
问题描述
我如何在拉姆达上最好地创建这个功能?
How would I best create this function in Ramda?
function get_list (value) {
return [
first_transform(value),
second_transform(value)
]
}
get_list(12)
我猜这是 map 功能。
推荐答案
假设您的函数已经在列表中:
Assuming your functions are already in a list:
transforms = [first_transform, second_transform];
第一个选项是使用 R.juxt
,它通过创建一个应用给函数赋予新函数接收的值。
The first option is to use R.juxt
, which does pretty much exactly what you're after by creating a new function that applies the list of given functions to the values received by the new function.
get_list = R.juxt(transforms);
另一个选项是 R.ap
,它将函数列表应用于值列表。可以使用 R.of
来包装
$ b
Another option is R.ap
, which applies a list of functions to a list of values. R.of
can be used to wrap the value in an array.
get_list = R.compose(R.ap(transforms), R.of);
最后, R.map
可用于接收列表中的每个函数并返回将其应用于该值的结果。
Or lastly, R.map
could be used to receive each function in the list and return the result of applying it to the value.
get_list = value => R.map(fn => fn(value), transforms);
这篇关于将函数列表应用于Ramda中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!