Skip to content

Instantly share code, notes, and snippets.

@roman
Created October 22, 2011 02:18
Show Gist options
  • Save roman/1305449 to your computer and use it in GitHub Desktop.
Save roman/1305449 to your computer and use it in GitHub Desktop.
Haskell Iteratees in clojure (naive impl)
(ns iteratee
(:require [clojure.contrib.types :as adt]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(adt/defadt ::stream
eof
(chunks xs))
(adt/defadt ::iteratee
(yield value stream)
(continue consumer)
(error e))
(defn sum [xs] (reduce + 0 xs))
(defn $$ [enum it] (enum (it)))
(defn run_ [it]
(adt/match it
(error e) e
(yield result _) result
(continue consumer)
(do
(let [next-it (consumer eof)]
(do
(adt/match next-it
(error e) e
(yield result _) result
(continue _) (do
(println "missbehaving iteratee")
nil)))))))
(defn enum-seq [chunk-size xs0 it]
(let [ [a xs] (split-at chunk-size xs0) ]
(if (seq a)
(adt/match it
(error _) it
(yield _ _) it
(continue consumer)
(recur chunk-size xs (consumer (chunks a))))
it)))
(defn sum-it []
(letfn [(go [acc stream]
(adt/match stream
eof
(yield acc eof)
(chunks xs)
(do
(continue (partial go (+ (sum xs) acc))))))]
(continue (partial go 0))))
(defn -main [& args]
(run_ ($$ (partial enum-seq 5 [1 2 3 4 5 6]) sum-it)))
@tavisrudd
Copy link

tavisrudd commented Oct 22, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment