Skip to content

Instantly share code, notes, and snippets.

@kylefeng
Created August 12, 2013 08:45
Show Gist options
  • Save kylefeng/6209214 to your computer and use it in GitHub Desktop.
Save kylefeng/6209214 to your computer and use it in GitHub Desktop.
(import '[java.io PushbackReader])
(defn make-name [f]
(str "memo/" (clojure.string/replace (str f) #" " "") "-memo-function.clj"))
(defn- open-memo-file [name]
(let [name (make-name name)]
(try
(with-open [r (clojure.java.io/reader name)]
(read (PushbackReader. r)))
(catch Exception e
(do
(println (str "File don't exist, will create file " name))
{})))))
(defn fake [input name]
(fn [& args]
(let [stored (open-memo-file name)]
(if-let [v (get stored args)]
v
(let [result (apply input args)
new-file (merge stored {args result})]
(with-open [w (clojure.java.io/writer (make-name name))]
(binding [*out* w
*print-dup* true]
(pr new-file)))
result)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment