Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

I guess this is the inverse of the map function.

like image 630
MattMS Avatar asked Dec 28 '15 04:12

MattMS


People also ask

Is ramda better than Lodash?

Ramda is generally a better approach for functional programming as it was designed for this and has a community established in this sense. Lodash is generally better otherwise when needing specific functions (esp. debounce ).

What is Ramda in JavaScript?

Ramda is a practical functional library for JavaScript programmers. The library focuses on immutability and side-effect free functions. Ramda functions are also automatically curried, which allows to build up new functions from old ones simply by not supplying the final parameters.


1 Answers

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.

get_list = R.juxt(transforms);

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);

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);
like image 113
Scott Christopher Avatar answered Oct 11 '22 13:10

Scott Christopher