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

take-while

clojure.core

  • (take-while pred coll)
Returns a lazy sequence of successive items from coll while
(pred item) returns true. pred must be free of side-effects.

2 Examples top

  • ;; Calculate the sum of all numbers under 1000:
    user=> (reduce + (take-while (partial > 1000) (iterate inc 0)))
    499500
  • user=> (take-while neg? [-2 -1 0 1 2 3])
    (-2 -1)
    
    user=> (take-while neg? [-2 -1 0 -1 -2 3])
    (-2 -1)
    
    user=> (take-while neg? [ 0 1 2 3])
    ()
    
    user=> (take-while neg? [])
    ()
    
    user=> (take-while neg? nil)
    ()
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:2162 top

(defn take-while
  "Returns a lazy sequence of successive items from coll while
  (pred item) returns true. pred must be free of side-effects."
  {:added "1.0"}
  [pred coll]
  (lazy-seq
   (when-let [s (seq coll)]
       (when (pred (first s))
         (cons (first s) (take-while pred (rest s)))))))
Vars in clojure.core/take-while: cons defn first lazy-seq rest when when-let

Comments top

No comments for take-while. Log in to add a comment.