简体   繁体   English

将函数列表应用于Ramda中的值

[英]Apply a list of functions to a value in Ramda

How would I best create this function in Ramda ? 我怎样才能在Ramda中最好地创建这个功能?

function get_list (value) {
  return [
    first_transform(value),
    second_transform(value)
  ]
}

get_list(12)

I guess this is the inverse of the map function. 我猜这是地图功能的反面。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM