Created
August 14, 2009 19:40
-
-
Save fogus/168053 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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