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:6290 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"
   :static true}
  ([n coll]
     (partition-all n n coll))
  ([n step coll]
     (lazy-seq
      (when-let [s (seq coll)]
        (let [seg (doall (take n s))]
          (cons seg (partition-all n step (nthrest s step))))))))
Vars in clojure.core/partition-all:
Used in 0 other vars

Comments top

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