[英]Apply a list of functions to a value in Ramda
You've got a few options for this. 你有几个选择。
Assuming your functions are already in a list: 假设您的功能已经在列表中:
transforms = [first_transform, second_transform];
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. 第一个选项是使用
R.juxt
,它通过创建一个将给定函数列表应用于新函数接收的值的新函数,几乎完全符合您的要求。
get_list = R.juxt(transforms);
Another option is R.ap
, which applies a list of functions to a list of values. 另一个选项是
R.ap
,它将函数列表应用于值列表。 R.of
can be used to wrap the value in an array. R.of
可用于将值包装在数组中。
get_list = R.compose(R.ap(transforms), R.of);
Or lastly, R.map
could be used to receive each function in the list and return the result of applying it to the value. 或者最后,
R.map
可用于接收列表中的每个函数并返回将其应用于值的结果。
get_list = value => R.map(fn => fn(value), transforms);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.