Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
Created May 8, 2010 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalmarczyk/394219 to your computer and use it in GitHub Desktop.
Save michalmarczyk/394219 to your computer and use it in GitHub Desktop.
;;; do NOT try to print seqs partitioned by the following...
;;; example usage follows code
(defn partition-by* [f r]
(let [igen (fn igen [l s]
(lazy-seq
(if-let [current (first s)]
(if (= l (f current))
(do (swap! r next)
(cons (first s)
(igen l (next s))))))))]
(lazy-seq
(cons
(igen (f (first @r)) @r)
(partition-by* f r)))))
(defn funky-partition-by [f coll]
(partition-by* f (atom (seq coll))))
;;; from my REPL:
(comment
user> (let [foo (partition-by* even? (atom (range 10)))]
(dorun (first foo))
(second foo))
(1)
user> (let [foo (partition-by* even? (atom (range 10)))]
(dorun (first foo))
(dorun (second foo))
(nth foo 2))
(2)
user> (let [foo (funky-parition-by even? (range 10))]
(dorun (first foo))
(dorun (second foo))
(nth foo 2))
(2)
user> (funky-parition-by even? (range 10))
; Evaluation aborted.
user> (let [foo (funky-parition-by even? (range 10))]
(doseq [i (range 9)]
(dorun (nth foo i)))
(nth foo 10))
(9)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment