some

added
1.0

ns
clojure.core

type
function

(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)

                ;; 2 is even, so `some` stops there, 3 and 4 are never tested
(some even? '(1 2 3 4))
;;=> true

;; they are all odd, so not true, i.e. nil
(some even? '(1 3 5 7))
;;=> nil
            
                (some true? [false false false])
;;=> nil

(some true? [false true false])
;;=> true

(some true? [true true true])
;;=> true

            
                (some #(= 5 %) [1 2 3 4 5])
;;=> true

(some #(= 5 %) [6 7 8 9 10])
;;=> nil
            
                ;; the first logical true value is returned, i.e. anything but nil and false
;; when return nil if its predicate is logical false.
(some #(when (even? %) %) '(1 2 3 4))
;;=> 2
            
                ;; a hash acts as a function returning nil when the
;; key is not present and the key value otherwise.
(some {2 "two" 3 "three"} [nil 3 2])
;;=> "three"

;; there is nothing special about the 'nil' in the collection
;; other than it is not found in the hash.
(some {nil "nothing" 2 "two" 3 "three"} [nil 3 2])
;;=> "nothing"

;; the hash (as function) returns a nil for the key of '3';
(some {2 "two" 3 nil} [nil 3 2])
;;=> "two"
            
                ;; some can be used as a substitute for (first (filter ...
;; in most cases.

(first (filter even? [1 2 3 4]))
;;=> 2

;; 'some' returns exactly one item (nil if nothing is found)
(some #(if (even? %) %) [1 2 3 4])
;;=> 2

            
                ;; find a whether a word is in a list of words.
(def word "foo")
(def words ["bar" "baz" "foo" ""])
(some (partial = word) words)
;;=> true
            
                ;; here we see sets being used as a predicates
;; the first member of the collection that appears in the set is returned

(some #{2} (range 0 10))      ;;=> 2
(some #{6 2 4} (range 0 10))  ;;=> 2
(some #{2 4 6} (range 3 10))  ;;=> 4
(some #{200} (range 0 10))    ;;=> nil


            
                ;; be careful, 'nil' can occasionally be returned on success.
(#{nil} nil)
;;=> nil 

;; almost as troublesome is returning a false
(#{false} false)
;;=> false
            
                ;; if you have a case where the predicate arguments are fixed/known, 
;; but the predicate function isn't:
;; coll can supply the predicate function instead of the predicate arguments

;; define the function 'not equal' (ne)
(defn ne [n1 n2] (not= n1 n2))
;;=> #'user/ne

(some #(% 3 7) (list ne))
;;=>true

(some #(% 3 3) (list ne))
;;=>nil



            
                ;; extending the previous example: supplying multiple functions.
(defn ne [n1 n2] (not= n1 n2)) 
;;=> #'user/ne

;; function to check if the sum is less than 'limit'
(defn sumlt [limit n1 n2] (> limit (+ n1 n2))) 
;;=>'user/sumlt

(some #(% 3 7) (list ne #(sumlt 10 %1 %2)))   
;;=>true

(some #(% 3 3) (list ne #(sumlt 10 %1 %2)))   
;;=>true

(some #(% 7 7) (list ne #(sumlt 10 %1 %2)))   
;;=>nil

;; same, but one of the functions returns a value instead a boolean.
(some #(% 3 7) (list ne (fn [n1 n2] (+ n1 n2))))                 
;;=>true

(some #(% 7 7) (list ne (fn [n1 n2] (+ n1 n2))))                 
;;=>14

;; the importance of order of the function list.
(some #(% 7 7) (list ne #(sumlt 10 %1 %2) (fn [n1 n2] (+ n1 n2)))) 
;;=>14

(some #(% 7 7) (list ne (fn [n1 n2] (+ n1 n2)) #(sumlt 10 %1 %2))) 
;;=>14

(some #(% 3 3) (list ne #(sumlt 10 %1 %2) (fn [n1 n2] (+ n1 n2)))) 
;;=>true

(some #(% 3 3) (list ne (fn [n1 n2] (+ n1 n2)) #(sumlt 10 %1 %2))) 
;;=>6

            
                ;;if you want to return the element the caused the predicate to return true
;;use the "and" function on the predicate and the argument:

(some #(and (even? %) %) [1 3 5 7 8 2])
;;=> 8