admin管理员组

文章数量:1201788

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.

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.

Share Improve this question asked Dec 19, 2015 at 22:45 Mateus FelipeMateus Felipe 1,1613 gold badges20 silver badges45 bronze badges 4
  • I tried to use reduce function, but it returns another function instead of the list. Can you give me an example? – Mateus Felipe 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
Add a comment  | 

5 Answers 5

Reset to default 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]]);

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

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]

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

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.

本文标签: javascriptAvoid double concat in RamdaStack Overflow