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.
5 Answers
Reset to default 19If 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
版权声明:本文标题:javascript - Avoid double concat in Ramda - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738568601a2100453.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
[r,r2,r3,r4].reduce( (a,b)=>a.concat(b) )
– dandavis Commented Dec 19, 2015 at 23:21[].concat(list1, list2, list3, list4)
. just as pure and seems cleaner. – trve.fahad Commented Sep 13, 2018 at 8:22