1.3.0 permalink 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:2502 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"
   :static true}
  [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:
Used in 0 other vars

Comments top

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