--- url: "https://martinfowler.com/articles/collection-pipeline/union.html" titulek: "union" h1: "union" popis: "" jazyk: "" kanonicka: "" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:35:24" znaku: 1347 slov: 190 kodovani: "utf-8" sha256: "b483e09a2b01ce1ccff99520ec44ecc0e218188ef1ac62ba7bb9aae72d512740" --- This page describes an operation in the collection pipeline pattern. For more context read: - [Collection Pipeline Article](/articles/collection-pipeline) : An article explaining the collection pipeline pattern - [Operation Catalog](/articles/collection-pipeline#op-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](intersection.html), union is a less natural operation on a collection pipeline. Instead it's more suited to [nested operator expressons](/articles/collection-pipeline/#NestedOperatorExpressions). Indeed it's even less useful as a full operator than intersection is, since it is easily built by composing [concat](concat.html) with [distinct](distinct.html) ruby… ``` [1,2,3].concat([3,4,5]).uniq # => [1, 2, 3, 4, 5] ``` clojure… ``` (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](/articles/collection-pipeline/#messy-infix-pipe). 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.