Skip to content

Instantly share code, notes, and snippets.

@fbmnds
Last active December 19, 2015 07:39
Show Gist options
  • Select an option

  • Save fbmnds/5919977 to your computer and use it in GitHub Desktop.

Select an option

Save fbmnds/5919977 to your computer and use it in GitHub Desktop.
Pacman metaphor
(require '[clojure.data.csv :as csv]
'[clojure.java.io :as io])
;; avoid REPL timeouts by large data output (during testing)
;;
(set! *print-length* 10)
;; download from https://s3.amazonaws.com/hw-sandbox/tutorial1/NYSE-2000-2001.tsv.gz
;; 812,990 data records, 40 MB on disk
;;
(def in-file "/path/to/datafile/NYSE-2000-2001.tsv")
(def in-seq (line-seq (io/reader in-file)))
;; yes it is:
(coll? in-seq)
;; Do-Nothing-Pacman
;;
(defn f [m]
(let [rem (:remaining m)]
(cond (nil? rem) {:end true}
(= 1 (count rem)) {:yield "OK"}
:else {:remaining (rest rem)})))
(defn parse [f coll]
(->> coll
(iterate f)
(take-while #(not (contains? % :end)))
(map :yield)
(filter #(not (nil? %)))))
(defn make-m [coll] (hash-map :remaining coll))
(defn measure [f n seq]
(time (first (parse f (make-m (take n seq))))))
;; you may want to start manually because of runtime duration:
;;
(measure f 10 in-seq)
;; ...
(measure f 100000 in-seq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment