Created
March 11, 2012 07:13
-
-
Save bmmoore/2015393 to your computer and use it in GitHub Desktop.
repeated-until, directly
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 repeated-until* [element done] | |
(reify | |
Reader | |
(read-bytes [this b] | |
(loop [b b, result []] | |
(let [[success x b] (read-bytes element b)] | |
(if success | |
(if (= x done) | |
[true result b] | |
(recur b (conj result x))) | |
[false | |
(compose-callback | |
this | |
(fn [result2 b2] | |
[true (concat result result2) b2])) | |
b])))) | |
Writer | |
(sizeof [_] nil) | |
(write-bytes [_ buf vs] | |
;; should perhaps do something smarter if size fixed. | |
(loop [chunks [], items vs] | |
(if-let [elt (first items)] | |
(recur (conj chunks (write-bytes element buf elt)) | |
(rest items)) | |
(concat (conj chunks (write-bytes element buf done)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment