1.3.0 permalink Arrow_down_16x16

some-fn

clojure.core

  • (some-fn p)
  • (some-fn p1 p2)
  • (some-fn p1 p2 p3)
  • (some-fn p1 p2 p3 & ps)
Takes a set of predicates and returns a function f that returns the first logical true value
returned by one of its composing predicates against any of its arguments, else it returns
logical false. Note that f is short-circuiting in that it will stop execution on the first
argument that triggers a logical true result against the original predicates.

3 Examples top

  • user=> ((some-fn even?) 1)
    false
    user=> ((some-fn even?) 2)
    true
    user=> ((some-fn even?) 1 2)
    true
    
  • ;; `some-fn` is useful for when you'd use `some` (to find out if any
    ;; values in a given coll satisfy some predicate), but have more than
    ;; one predicate. For example, to check if any values in a coll are
    ;; either even or less than 10:
    
    (or (some even? [1 2 3])
        (some #(< % 10) [1 2 3]))
    
    ;; but `some-fn` can save you some duplication here:
    
    ((some-fn even? #(< % 10)) 1 2 3)
    
    ;; Minor note: the former returns nil if it doesn't find
    ;; what it's looking for. The latter returns false.
  • ;;; http://en.wikipedia.org/wiki/Fizz_buzz
    (def fizzbuzz
      (some-fn #(and (= (mod % 3) 0) (= (mod % 5) 0) "FizzBuzz")
               #(and (= (mod % 3) 0) "Fizz")
               #(and (= (mod % 5) 0) "Buzz")))
    
    (doseq [n (take 17 (rest (range)))]
      (println (or (fizzbuzz n) n)))
    
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz
    16
    17
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:6421 top

(defn some-fn
  "Takes a set of predicates and returns a function f that returns the first logical true value
  returned by one of its composing predicates against any of its arguments, else it returns
  logical false. Note that f is short-circuiting in that it will stop execution on the first
  argument that triggers a logical true result against the original predicates."
  {:added "1.3"}
  ([p]
     (fn sp1
       ([] nil)
       ([x] (p x))
       ([x y] (or (p x) (p y)))
       ([x y z] (or (p x) (p y) (p z)))
       ([x y z & args] (or (sp1 x y z)
                           (some p args)))))
  ([p1 p2]
     (fn sp2
       ([] nil)
       ([x] (or (p1 x) (p2 x)))
       ([x y] (or (p1 x) (p1 y) (p2 x) (p2 y)))
       ([x y z] (or (p1 x) (p1 y) (p1 z) (p2 x) (p2 y) (p2 z)))
       ([x y z & args] (or (sp2 x y z)
                           (some #(or (p1 %) (p2 %)) args)))))
  ([p1 p2 p3]
     (fn sp3
       ([] nil)
       ([x] (or (p1 x) (p2 x) (p3 x)))
       ([x y] (or (p1 x) (p2 x) (p3 x) (p1 y) (p2 y) (p3 y)))
       ([x y z] (or (p1 x) (p2 x) (p3 x) (p1 y) (p2 y) (p3 y) (p1 z) (p2 z) (p3 z)))
       ([x y z & args] (or (sp3 x y z)
                           (some #(or (p1 %) (p2 %) (p3 %)) args)))))
  ([p1 p2 p3 & ps]
     (let [ps (list* p1 p2 p3 ps)]
       (fn spn
         ([] nil)
         ([x] (some #(% x) ps))
         ([x y] (some #(or (% x) (% y)) ps))
         ([x y z] (some #(or (% x) (% y) (% z)) ps))
         ([x y z & args] (or (spn x y z)
                             (some #(some % args) ps)))))))
Vars in clojure.core/some-fn:
Used in 0 other vars

Comments top

No comments for some-fn. Log in to add a comment.