Created
May 8, 2010 01:27
-
-
Save michalmarczyk/394219 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
;;; 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