Short-circuiting version of reduce - see http://jkkramer.wordpress.com/2011/03/29/clojure-python-side-by-side/
(defn tails [s] | |
"The sequence of sequences starting at each position in the given sequence 's'." | |
(lazy-seq (when-let [s (seq s)] | |
(cons s (tails (rest s)))))) | |
(defn reduce-true [f val coll] | |
"A short-circuiting version of 'reduce'. Stops evaluation when the | |
accumulated value is logically false." | |
(let [v (->> (reductions f val coll) | |
(tails) | |
(take-while first) | |
(last))] | |
(if (empty? (rest v)) (first v)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment