dedupe removes consecutive duplicate values from a collection while preserving repetitions separated by another value. It returns a lazy sequence when given a collection and can also produce a transducer when called without one. section 10.8
Consecutive versus separated duplicates
(dedupe [1 2 1 1 3 2 4 1]) ;; (1 2 1 3 2 4 1) The adjacent 1s collapse to one value, while the later 1s remain because other values separate them. section 10.8
Use dedupe with these contract details in mind:
- Accepts collection inputs, including sets and maps, but not transients; the collection argument is optional and may be nil. section 10.8
- Uses Clojure extended equality when deciding whether adjacent values match, including compatibility across scalar and collection values. section 10.8
- Differs from distinct by removing only contiguous duplicates; distinct removes all repeated values, regardless of position. section 10.8
(sequence
(comp
(map range)
cat
(distinct)) ; #1
(range 10))
;; (0 1 2 3 4 5 6 7 8)
(sequence
(dedupe) ; #2
[1 1 1 2 1 1 1 3 1 1])
;; (1 2 1 3 1)
The transducer form composes dedupe with other sequence transformations and removes only adjacent repetitions in the resulting stream. section 7.2
