Created
August 12, 2013 08:45
-
-
Save kylefeng/6209214 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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