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

partition-by

clojure.core

  • (partition-by f coll)
Applies f to each value in coll, splitting it each time f returns
a new value. Returns a lazy seq of partitions.

3 Examples top

  • user=> (partition-by #(= 3 %) [1 2 3 4 5])
    ((1 2) (3) (4 5))
  • user=> (partition-by odd? [1 1 1 2 2 3 3])
    ((1 1 1) (2 2) (3 3))
    
    user=> (partition-by even? [1 1 1 2 2 3 3])
    ((1 1 1) (2 2) (3 3))
    
  • ;; (this is part of a solution from 4clojure.com/problem 30)
    user=> (partition-by identity "Leeeeeerrroyyy")
    ((\L) (\e \e \e \e \e \e) (\r \r \r) (\o) (\y \y \y))
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:5554 top

(defn partition-by 
  "Applies f to each value in coll, splitting it each time f returns
   a new value.  Returns a lazy seq of partitions."
  {:added "1.2"}
  [f coll]
  (lazy-seq
   (when-let [s (seq coll)]
     (let [fst (first s)
           fv (f fst)
           run (cons fst (take-while #(= fv (f %)) (rest s)))]
       (cons run (partition-by f (drop (count run) s)))))))
Vars in clojure.core/partition-by: cons count defn drop lazy-seq let run when-let
Used in 2 other vars partition-by partition-by

Comments top

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