You're viewing version 1.2.0 of partition-all. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

partition-all

clojure.core

  • (partition-all n coll)
  • (partition-all n step coll)
Returns a lazy sequence of lists like partition, but may include
partitions with fewer than n items at the end.

3 Examples top

  • user=> (partition 4 [0 1 2 3 4 5 6 7 8 9])
    ((0 1 2 3) (4 5 6 7))
    
    user=> (partition-all 4 [0 1 2 3 4 5 6 7 8 9])
    ((0 1 2 3) (4 5 6 7) (8 9))
    
  • user=> (partition-all 2 4 [0 1 2 3 4 5 6 7 8 9])
    ((0 1) (4 5) (8 9))
  • Caution: Partitioning lazy sequence code freeze
    
    (def l [1 2 3 4 5])
    ;create a simple lazy sequence function testing only
    ;(rdr l) returns a lazy sequence from l
    (def rdr (fn reader[x] (cons (first x) (lazy-seq (reader  (rest x))))))
    
    ;the line below will freeze
    (doall (partition-all 2 (rdr l)) )
    
    ;add-in a take-while statement do exit the lazy sequence on nil
    (doall (partition-all 2 (take-while (complement nil?) (rdr l))))
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:5599 top

(defn partition-all
  "Returns a lazy sequence of lists like partition, but may include
  partitions with fewer than n items at the end."
  {:added "1.2"}
  ([n coll]
     (partition-all n n coll))
  ([n step coll]
     (lazy-seq
      (when-let [s (seq coll)]
        (cons (take n s) (partition-all n step (drop step s)))))))
Vars in clojure.core/partition-all: cons defn drop lazy-seq take when-let
Used in 2 other vars partition-all partition-all

Comments top

No comments for partition-all. Log in to add a comment.