Created
October 15, 2010 11:45
-
-
Save hiredman/628066 to your computer and use it in GitHub Desktop.
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 sieve [f a-seq] | |
(lazy-seq | |
(let [[x & xs] a-seq] | |
(when (or x (seq xs)) | |
(let [[new-x new-f] (f x)] | |
(if new-x | |
(cons x (sieve new-f xs)) | |
(sieve new-f xs))))))) | |
;;re-implement filter | |
(defn sfilter [f a-seq] | |
(sieve (fn thisfn [x] [(f x) thisfn]) a-seq)) | |
;;drop the nth element from a seq | |
(defn drop-nth [n a-seq] | |
(sieve | |
(partial (fn thisfn [m x] | |
[(not= m n) (partial thisfn (inc m))]) | |
0) | |
a-seq)) | |
;;stateful, yet functional sieve | |
(defn drop-all-odds-after-five [a-seq] | |
(sieve | |
(partial (fn thisfn [seen-five? x] | |
[(if seen-five? (even? x) true) | |
(partial thisfn (or seen-five? (= x 5)))]) | |
false) | |
a-seq)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment