Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created August 24, 2012 14:55
Show Gist options
  • Save mwmitchell/3451628 to your computer and use it in GitHub Desktop.
Save mwmitchell/3451628 to your computer and use it in GitHub Desktop.
filters
;; 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