ClojureDocs

导航

命名空间

partition-all

clojure.core

1.2 中新增 ()
  • (partition-all n)
  • (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.  Returns a stateful
transducer when no collection is provided.
4 Examples
(partition 4 [0 1 2 3 4 5 6 7 8 9])
;;=> ((0 1 2 3) (4 5 6 7))

(partition-all 4 [0 1 2 3 4 5 6 7 8 9])
;;=> ((0 1 2 3) (4 5 6 7) (8 9))
(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
;; Edit: Only because (rdr l) is buggy and returns an infinite sequence!

(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))))
;; When the step parameter is provided, the starting index value of the first item in the 
;; next group steps relative to the current index value
;; 提供step参数时,下一个分组的第一项的起始索引值相对于当前索引值的步进(+ pre-index step)
(println (partition-all 2 3 '(0 1 2 3 4 5 6 7 8 9)))
;; => ((0 1) (3 4) (6 7) (9))
(println (partition-all 4 3 '(0 1 2 3 4 5 6 7 8 9)))
;; => ((0 1 2 3) (3 4 5 6) (6 7 8 9) (9))
;; When n is equal to step, it is equivalent to no step parameter
;; 当n与step相等,相当于无step参数
(println (partition-all 3 3 '(0 1 2 3 4 5 6 7 8 9)))
;; => ((0 1 2) (3 4 5) (6 7 8) (9))
See Also

Returns a lazy sequence of lists of n items each, at offsets step apart. If step is not supplied, ...

Added by boxie

Applies f to each value in coll, splitting it each time f returns a new value. Returns a lazy se...

Added by boxie
1 Note
    By , created 315 days ago, updated 315 days ago

    be aware of the case of n is 0

    (partition-all 0 '(1 2 3))

    will cause repl blocked and cpu 100%