Skip to content

Instantly share code, notes, and snippets.

@hiredman hiredman/scratch.clj
Created Oct 15, 2010

Embed
What would you like to do?
(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
You can’t perform that action at this time.