liveBook · Manning

Take

As explained in Clojure: The Essential Reference

take is a Clojure function that returns the first specified number of elements from a collection or returns a transducer that keeps that prefix.

In Clojure, take keeps the first n elements of a sequential input and discards the remainder. It is useful for limiting a sequence, inspecting a prefix, or selecting a record’s leading fields. section 10.2

The function supports both ordinary sequence processing and transducer composition:

  • (take n coll) returns a sequence containing at most the first n elements. section 10.2
  • (take n) returns a transducer that keeps the prefix and terminates reduction after n elements. section 7.2
  • n may be positive, zero, negative, integer, or floating-point; negative values behave like zero, and coll may be nil or any seqable input. section 10.2
  • A non-sequential coll violates the sequence contract and raises IllegalArgumentException. section 10.2

Selecting leading fields

Taking the first three values from each HTTP log record makes its status code, day, and month available for filtering: (let [[code _ month] (take 3 record)] ...). Applied to the sample messages, this identifies the three March errors.

Composing a transducer

When the collection argument is omitted, take composes with other transformations: (transduce (comp (drop 3) (map inc) (take 4)) + (range 10)) processes only the selected prefix after dropping three items and incrementing values. section 7.2

Table 10.2. A summary of the functions available to extract a portion of a sequence.

Name

Description

Transducible?

Fully lazy?

drop

Drops the first n elements.

Yes

Yes

drop-while

Drops items until the given predicate returns false.

Yes

Yes

drop-last

Drops the last n elements (default to 1).

No

Yes

take

Keeps the first n elements, drops the rest.

Yes

Yes

take-while

Keeps element until the given predicate returns false.

Yes

Yes

take-last

Keeps the last n elements (no default for n).

No

No

nthrest

Like drop with inverted arguments. Never returns nil.

No

No

nthnext

Like nthrest but returns nil if beyond input length.

No

No

The comparison table places take among sequential selectors, contrasting its prefix-preserving behavior, transducer support, and full laziness with related functions. section 10.2

How Clojure: The Essential Reference covers it

How deep it goes
Explains arities, outputs, laziness, transducer termination, numeric edge cases, and the invalid-input exception.
Examples
Demonstrates record-field extraction, a composed transducer, lazy realization, and comparison with vector subvec prefixing.
What it assumes
Assumes familiarity with sequential inputs and introduces take’s optional collection argument for transducer use.
What you can do after
Apply it via Clojure sequence calls or transducer chains to retain a bounded prefix of data.
How it connects
Connects take with sequences, transducers, drop, take-while, take-last, map, and reduction.

Also explained in

Compare how 4 books explain take →

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.