Skip to content

Instantly share code, notes, and snippets.

@bmmoore
Created March 11, 2012 07:13
Show Gist options
  • Select an option

  • Save bmmoore/2015393 to your computer and use it in GitHub Desktop.

Select an option

Save bmmoore/2015393 to your computer and use it in GitHub Desktop.

Revisions

  1. bmmoore created this gist Mar 11, 2012.
    25 changes: 25 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    (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))))))))