chapter ten

10 Sequential Processing

 

This chapter is about functions and macros for sequential processing. These functions typically transform their input into a sequence (if it’s not already) and produce another sequence. Although sequential functions can be used with any collection type offering a sequential interface, they tend to perform at their best with pure sequential input/output.

Sequential processing can be broadly categorized as follows:

  • Splitting: isolate a consecutive portion of the sequence by index, number of items, or using a custom predicate. Functions like split belong to this group, and also drop, rest and their variations.
  • Selection: retrieve specific items from the sequence but not necessarily as a consecutive selection, for example take-nth or filter.
  • Transformation: apply a function to each item in the sequence to produce another sequence, like map, keep or mapcat.
  • Combining: combination of multiple sequences to form another sequence, like interpose or concat.
  • Chunking: process a sequence by groups of multiple elements instead of one at a time. Clojure provides a small API to create chunked sequences described in the chunk-cons section.

Some functions like “first, second and last”, “map and map-indexed” or “filter and remove” also belong here, but they have given specific emphasis in the "Basic Constructs" chapter.

10.1  rest, next, fnext, nnext, ffirst, nfirst and butlast

functions since 1.0

Seq-in-seq-out, Sequential processing

10.2  drop, drop-while, drop-last, take, take-while, take-last, nthrest, nthnext

10.3  keep and keep-indexed

10.4  mapcat

10.5  interpose and interleave

10.6   partition, partition-all and partition-by

10.7  flatten

10.8  distinct, dedupe and distinct?

10.9  take-nth

10.10  split-at and split-with

10.11  when-first