liveBook · Manning

Dedupe

As explained in Clojure: The Essential Reference

dedupe is a Clojure function that removes consecutive duplicate values while retaining separated repetitions.

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

How Clojure: The Essential Reference covers it

How deep it goes
Defines equality, input contracts, lazy output, transducer arity, and contiguous-only removal, without detailing implementation internals.
Examples
Demonstrates vector processing, separated repetitions, and transducer composition over generated sequences.
What you can do after
Apply dedupe to collapse runs of repeated values or compose its transducer with other sequence transformations.
How it connects
Contrasts dedupe with distinct and distinct?, and connects it to transducers, lazy sequences, and Clojure equality.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.