Created
August 24, 2012 14:55
-
-
Save mwmitchell/3451628 to your computer and use it in GitHub Desktop.
filters
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
;; a function that provides the ability to filter a collection using multiple predicate functions, *lazily*. | |
;; The predicate functions must accept 2 args: | |
;; - the first arg is the accumulating collection (think "reduce"). | |
;; - the second arg is the current item in the collection. | |
;; The return value is a reduced version of the input | |
;; | |
(defn filters [res items & fns] | |
(last (reductions | |
(fn [r i] | |
(if (every? #(% r i) fns) (conj r i) r)) | |
res | |
items))) | |
(take 100 | |
(filters #{} | |
(concat (range -250000 250000) (range -250000 250000)) | |
(fn [r i] (not (r i))) | |
(fn [_ i] (pos? i)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment