title: Clojure and HBase: Iterate Lazily over a Scan
body: <p>Lets say I want to print the output of an hbase table scan in clojure. </p>
  
  <pre><code>(defmulti scan (fn [table &amp; args] (map class args)))
  
  (defmethod scan [java.lang.String java.lang.String] [table start-key end-key]
      (let [scan (Scan. (Bytes/toBytes start-key) (Bytes/toBytes end-key))]
          (let [scanner (.getScanner table scan)]
              (doseq [result scanner]
                  (prn
                      (Bytes/toString (.getRow result))
                      (get-to-map result))))))
  </code></pre>
  
  <p>where get-to-map turns the result into a map.  It could be run like this:</p>
  
  <pre><code>(hbase.table/scan table "key000001" "key999999")
  </code></pre>
  
  <p>But what if I want to let the user do something with the scan results?  I could allow them to pass a function in as a callback to be applied to each result.  But my question is this: what do I return if I want the user to be able to lazily iterate over the each result</p>
  
  <pre><code>(Bytes/toString (.getRow result))
  (get-to-map result)
  </code></pre>
  
  <p><em>and</em> not retain the previous results, as might happen in a simplistic implimentation with lazy-seq.  </p>