Skip to content

Instantly share code, notes, and snippets.

@fogus
Created August 14, 2009 19:32
Show Gist options
  • Save fogus/168046 to your computer and use it in GitHub Desktop.
Save fogus/168046 to your computer and use it in GitHub Desktop.
(defn filter-collecting [predicate collector & lists]
(lazy-seq
(loop [lists lists out []]
(if (empty? (first lists))
(reverse out)
(let [heads (map first lists)]
(if (apply predicate heads)
(recur (map rest lists) (cons (apply collector heads) out))
(recur (map rest lists) out)))))))
;; usage:
;; (filter-collecting
;; (fn [x y] (< x y))
;; (fn [x y] (+ x y))
;; '(1 7 3 9)
;; '(5 5 5 5))
;; ==> (6 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment