7

Trying to figure out Ramda here. Does Ramda have a method to turn arguments to a list? E.g:

R.argumentsToList(1)(2) => [1, 2]

The problem that I'm actually facing is that my function is passed 2 arguments and I need to work with both arguments, yet R.compose only allows the rightmost function to work with multiple arguments. Turning my arguments to a list would allow me to bypass this - or would that be a bad approach?

Practical problem: I want to pass 2 strings into my function. Those should be turned into a merged object, e.g:

f('firstString', 'second') => {firstString: 'firstString', second: 'second'}

I know that I can use R.merge and R.objOf, but I don't know how to handle and work with the 2 arguments that are passed to the function.

Oliver
  • 3,981
  • 2
  • 21
  • 35
  • `arguments` already is a sort of list. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments – Jonathan Dec 06 '15 at 10:42

2 Answers2

16

Creating a list function is one of the recipes in the Ramda Cookbook. It turns out to be fairly simple:

var list = R.unapply(R.identity);
list(1, 2, 3); // => [1, 2, 3]

This is based on the simple unapply, which converts a variadic function into one that accepts an array.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
2

I managed to do this with R.useWith:

R.useWith(R.concat, [R.of, R.of])

However, I'm not sure if this is the easiest/best solution for this, because I'm very inexperienced. Maybe someone with more experience can share their knowledge?

Oliver
  • 3,981
  • 2
  • 21
  • 35
  • This works, but only for two arguments. The answer I just gave works for any number, and is somewhat simpler. Nonetheless, nice job in coming up with this one! – Scott Sauyet Dec 07 '15 at 06:41