--- url: "https://martinfowler.com/articles/collection-pipeline/difference.html" titulek: "difference" h1: "difference" popis: "" jazyk: "" kanonicka: "" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:35:18" znaku: 1572 slov: 240 kodovani: "utf-8" sha256: "4734fae25d02b3338e1e47e76c26560decf3a0e987909f1fb449c8f50580afb5" --- 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. # difference Remove the contents of the supplied list from the pipeline ruby… ``` [1,1,2,2,3,4] - [1,3] # => [2, 2, 4] ``` clojure… ``` (remove #{1 3} [1 1 2 2 3 4]) ;; => (2 2 4) ``` Difference, as an operator, is something that's primarily used in the context of a [nested operator expresson](/articles/collection-pipeline/#NestedOperatorExpressions), but is also useful in a pipeline. Strictly it isn't needed if you have [filter](filter.html) since you can always use an appropriately configured filter to remove a given list of elements. (Indeed this is what the clojure example is actually doing since a clojure set can be used as a function which tests its argument to see if it's a member of the list.) ruby… ``` [1,1,2,2,3,4].reject {|i| [1,3].include? i} # => [2, 2, 4] ``` In Ruby's case, using reject is more verbose but avoids the [messy tangles](/articles/collection-pipeline/#messy-infix-pipe) that can appear when using infix operators in pipelines Difference is often seen as a set operation, but commonly these kinds of operations do attempt to remove duplicates. (Clojure does have a proper set difference function in clojure.set, but it only works on sets and it's argument ordering doesn't work with the threading operator.)