1.3.0 permalink Arrow_down_16x16
  • (some pred coll)
Returns the first logical true value of (pred x) for any x in coll,
else nil. One common idiom is to use a set as pred, for example
this will return :fred if :fred is in the sequence, otherwise nil:
(some #{:fred} coll)

8 Examples top

  • user=> (some even? '(1 2 3 4))
    true
    user=> (some even? '(1 3 5 7))
    nil
  • user=> (some true? [false false false])
    nil
    user=> (some true? [false true false])
    true
    user=> (some true? [true true true])
    true
    
  • user=> (some #(= 5 %) [1 2 3 4 5])
    true
    user=> (some #(= 5 %) [6 7 8 9 10])
    nil
  • user=> (some #(when (even? %) %) '(1 2 3 4))
    2
  • user=> (some {2 "two" 3 "three"} [nil 3 2])
    "three"
  • ;; some can be used as a substitute for (first (filter ...
    ;; in most cases
    
    user=> (first (filter even? [1 2 3 4]))
    2
    user=> (some #(if (even? %) %) [1 2 3 4])
    2
    user=>
  • ;; find a whether a word is in a list of words.
    (def word "foo")
    (some (partial = word) words)
  • user=> (some #{2} (range 0 10))
    2
    
    user=> (some #{200} (range 0 10))
    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:2379 top

(defn some
  "Returns the first logical true value of (pred x) for any x in coll,
  else nil.  One common idiom is to use a set as pred, for example
  this will return :fred if :fred is in the sequence, otherwise nil:
  (some #{:fred} coll)"
  {:added "1.0"
   :static true}
  [pred coll]
    (when (seq coll)
      (or (pred (first coll)) (recur pred (next coll)))))
Vars in clojure.core/some:
Used in 0 other vars

Comments top

1 comment(s) for some.

Be careful about using sets as predicates if you don't know what is in the set. In particular,

(#{nil} nil)
is
nil
and
(#{false} false)
is
false
Consider using
contains?
instead.

Log in to add a comment.