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.
union
returns elements in this or the supplied collection, removing duplicates
Like intersection, union is a less natural operation on a collection pipeline. Instead it's more suited to nested operator expressons. Indeed it's even less useful as a full operator than intersection is, since it is easily built by composing concat with distinct
[1,2,3].concat([3,4,5]).uniq # => [1, 2, 3, 4, 5]
(distinct (concat [1 2 3] [3 4 5])) ;; => (1 2 3 4 5)
Some languages, like ruby, provide an infix operator for untion (in Ruby it's “|”) but as with any infix operator, it doesn't work well with pipelines.
As with intersection, there is a union function in Clojure's set namespace, but using it requires you to turn convert any sequence into a set and back again, so the combination of distinct and concat is usually better.