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.
difference
Remove the contents of the supplied list from the pipeline
[1,1,2,2,3,4] - [1,3] # => [2, 2, 4]
(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, but is also useful in a pipeline.
Strictly it isn't needed if you have filter 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 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.)