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"
