9

My question is simple. I started with Ramda recently, and I like it, for it is pure functional. I have a little issue with concat function, as it only accepts two lists as arguments. So, if I need to concat three lists or more, I have to chain functions like this: concat(list1, concat(list2, concat(list3, list4))) (for a four lists concatenation). Is there any better way to do this, that I don't know? Thanks.

4
  • I tried to use reduce function, but it returns another function instead of the list. Can you give me an example? Commented Dec 19, 2015 at 22:56
  • 1
    bit.ly/1QRXCGf
    – dandavis
    Commented Dec 19, 2015 at 23:00
  • 1
    also note that there's not much advantage to doing it all inline like that, so in that case, the natives work just fine and are shorter: bit.ly/1QRYqLg [r,r2,r3,r4].reduce( (a,b)=>a.concat(b) )
    – dandavis
    Commented Dec 19, 2015 at 23:21
  • 1
    how about just using native code. [].concat(list1, list2, list3, list4). just as pure and seems cleaner.
    – trve.fahad
    Commented Sep 13, 2018 at 8:22

5 Answers 5

19

If you want to concatenate a list of lists, you can reduce the list using R.concat with an empty list as the initial value.

const concatAll = R.reduce(R.concat, []);
concatAll([[1, 2], [3, 4], [5, 6]]);

Or just pass the lists directly to R.reduce.

R.reduce(R.concat, [], [[1, 2], [3, 4], [5, 6]]);

If you want to create a new variadic function that takes each list as a separate argument, then you can wrap the function with R.unapply.

const concatAll_ = R.unapply(R.reduce(R.concat, []));
concatAll_([1, 2], [3, 4], [5, 6]);

Ramda also has an R.unnest function, which when given a list of lists will concatenate them together.

R.unnest([[1, 2], [3, 4], [5, 6]]);
1

I haven't used the Ramda library, but you appears to be using this in node.js from the documentation I've read in the link you posted. In that case, you can use the arguments variable in a function in node.js to write your own concat function that takes n lists as input. The arguments variable is essentially an array of the arguments inputted into the function.

function myConcat () {
  for (var i = 1; i < arguments.length; i++) {
    concat(arguments[0], arguments[i]);
  }
  return arguments[0];
};

In this case however, you would probably have to call it like:

list1 = myConcat(list1, list2, list3, list4);
2
  • 1
    Thanks for trying to help, but I was wishing a functional way to doing this. @dandavis got it right. Commented Dec 19, 2015 at 23:15
  • 1
    @MateusFelipe that's awesome. Sorry about that, I haven't used Ramda.js Commented Dec 19, 2015 at 23:19
1

Depending on exactly what you are concatenating and your environment (ES2015 required), you could do:

 const newList = [...list1, ...list2, ...list3];

Otherwise, you are stuck with multiple calls to concat, though you could make it a little cleaner with compose:

 const newList = compose(concat(list1), concat(list2), concat(list4))(list3);

really though you want to map or better, reduce:

 const newList = reduce((acc, x) => concat(acc, x), [list3], [list4, list2, list1]);

or that inner reduce function could look like:

 (acc, x) => [...acc, ...x]
1
  • @ScottChrisopher reduce is cleaner, forgot to pointfree-ify my concat. However, concat isn't really a great example of ramda's composability as we could just call call concat natively with [].concat(a, b, c), which s what the ES2015 code [...a, ...b, ...c] compiles to with babel
    – low_ghost
    Commented Dec 20, 2015 at 12:14
1

Using R.unnest with R.unapply allows you to call your function as a variadic:

const unnestAll = R.unapply(R.unnest)
unnestAll([1, 2], [3, 4], [5, 6, 7]) //=> [1, 2, 3, 4, 5, 6, 7]

A slight variation on Scott Cristopher's answer

See Example in repl

1

You can use flatten method this way:

R.flatten([list1, list2, list3, list4, ])

Technically you pass a single array, which contains all your lists. Then you just flatten them all into a single array.

Do NOT use this method if some of your lists might contain list themselves - they'll be flattened as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.