This page describes an operation in the collection pipeline pattern. For more context read:
- Collection Pipeline Article: An article explaining the collection pipeline pattern
- Operation Catalog: The list of selected operations that I have these pages for.
concat
Concatenates collections into a single collection
[1,2,3].concat([4,5]) # => [1, 2, 3, 4, 5]
(concat [1 2 3] [4 5]) ;; => (1 2 3 4 5)
If you want to concatenate more than two collections, you'll find different languages have different approaches.
[1,2,3].concat([4,5]).concat([6]) # => [1, 2, 3, 4, 5, 6]
(concat [1 2 3] [4 5] [6]) ;; => (1 2 3 4 5 6)
It's natural lisp style to allow functions to take a list of arguments, so that fits well for concatenation. Ruby's concat function only takes one argument, which you could see as a limitation, but is easily dealt with by chaining the concat.
Languages that support infix operators usually have an operator to concatenate collections, in Ruby it's “+”.
ruby…[1,2,3] + [4,5] + [6] # => [1, 2, 3, 4, 5, 6]
Whenever you're using infix operators, you need to group them with parentheses to get them to chain properly.
ruby…([1,2,3] + [4,5] + [6]).select(&:odd?) # => => [1, 3, 5]
And as usual with infix operators, they don't work well except at the head of the chain.