liveBook · Manning

Interpose

As explained in Clojure: The Essential Reference

interpose is a Clojure function that inserts a separator between successive elements of a collection and can also produce a transducer.

interpose inserts the same separator between successive elements of a collection. It is useful for formatting sequences, such as turning words or lines into comma- or delimiter-separated text. section 10.5

Use its two arities according to the desired processing style:

  • (interpose sep coll) returns a sequence with sep between input items. section 10.5
  • (interpose sep) returns a transducer that alternates input items with sep during reduction. section 10.5 section 7.2
  • The separator may be any expression or constant value, including nil; sep is required. section 10.5
  • Prefer interleave when alternating elements from multiple collections; it stops when the shortest input ends. section 10.5

Separating collection elements

(interpose :orange [:green :red :green :red]) ;; (:green :orange :red :orange :green :orange :red)

Formatting and transducing text

Both ordinary sequence processing and transduction can build delimited text: (apply str (interpose ", " grocery)) ;; "apple, banana, mango, other fruits" (transduce (interpose ", ") str grocery) ;; "apple, banana, mango, other fruits"

How Clojure: The Essential Reference covers it

How deep it goes
Defines both collection and transducer arities, separator requirements, and practical outputs without detailing implementation internals.
Examples
Demonstrates keyword separation, grocery-list formatting, string concatenation, and transduce.
What it assumes
Assumes familiarity with collections supporting the sequential interface and introduces the collection and transducer forms explicitly.
What you can do after
Apply it via Clojure sequence functions or transduce to insert delimiters into collections and reductions.
How it connects
Connects sequence processing, transducers, string concatenation, and interleave.

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.