Created
November 1, 2011 08:20
-
-
Save odf/1330143 to your computer and use it in GitHub Desktop.
Short-circuiting version of reduce - see http://jkkramer.wordpress.com/2011/03/29/clojure-python-side-by-side/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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