# HG changeset patch # User Robert McIntyre # Date 1331411050 21600 # Node ID 9864032ef3c89b12852ff20c20cd0d9217aed115 # Parent 3f4fdd270059532db28791ad9f1c4c4e7d86af52 cleaned up code and made it to the end of the title diff -r 3f4fdd270059 -r 9864032ef3c8 clojure/com/aurellem/gb_funs.clj --- a/clojure/com/aurellem/gb_funs.clj Sat Mar 10 00:32:11 2012 -0600 +++ b/clojure/com/aurellem/gb_funs.clj Sat Mar 10 14:24:10 2012 -0600 @@ -120,10 +120,10 @@ (play @current-state n))) (defn play-moves - ([state moves] - - ([moves] - (dorun (map (fn [move] (step @current-state move)) moves)))) + ([moves [prev state]] + (set-state! state) + (dorun (map (fn [move] (step @current-state move)) moves)) + [(concat prev moves) @current-state])) ;;;;;;;;;;; @@ -164,4 +164,12 @@ (defn DE [state] (nth (registers state) 4)) +;;;;;;;;;;;;;;; + +(defmacro defn-memo + [& forms] + (let [fun-name (first forms)] + `(do + (defn ~@forms) + (alter-var-root (var ~fun-name) memoize)))) \ No newline at end of file diff -r 3f4fdd270059 -r 9864032ef3c8 clojure/com/aurellem/title2.clj --- a/clojure/com/aurellem/title2.clj Sat Mar 10 00:32:11 2012 -0600 +++ b/clojure/com/aurellem/title2.clj Sat Mar 10 14:24:10 2012 -0600 @@ -23,17 +23,17 @@ (def scroll-text (partial advance [:b] [:a :b])) -(defn title [] - (->> [[] (root)] +(defn start [] [[] (root)]) + +(defn-memo title [start] + (->> start (advance [] [:a]) (advance [] [:start]) (advance [] [:a]) (advance [] [:start]))) -(def title-end (second (title))) - -(defn oak [] - (->> [[] title-end] +(defn-memo oak [start] + (->> start scroll-text scroll-text scroll-text @@ -49,23 +49,43 @@ scroll-text (advance [] [:a]))) -;; looks like it might need a monad here if this pattern continues +(defn-memo name-entry [start] + (->> start + (advance [] [:r] DE) + (play-moves + [[] + [:r] [] [:r] [] [:r] [] [:r] [] + [:r] [] [:r] [] [:r] [] [:d] [:a] + [:l] [] [:l] [] [:l] [] [:l] [] + [:l] [] [:l] [:a] [] [:r] [:a] + [:r] [] [:r] [] [:r] [] [:r] [] + [:r] [] [:d] [] [:d] [] [:d] [:a] + ]))) + +(defn-memo rival-name-entry [start] + (->> start + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + (advance [] [:d]) + (advance [] [:d]) + (advance [] [:a]))) -(def oak-end (second (oak))) - -(defn name-entry [] - (->> [[] oak-end] - (advance [] [:r] DE) - ((fn [[moves state]] - (play-moves - state - [[] - [:r] [] [:r] [] [:r] [] [:r] [] - [:r] [] [:r] [] [:r] [] [:d] [:a] - [:l] [] [:l] [] [:l] [] [:l] [] - [:l] [] [:l] [:a] [] [:r] [:a] - [:r] [] [:r] [] [:r] [] [:r] [] - [:r] [] [:d] [] [:d] [] [:d] [:a] - ]))))) - +(defn-memo finish-title [start] + (->> start + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text)) +(defn-memo intro [] + (-> (start) title oak name-entry rival-name-entry finish-title)) + + +;; TODO might be able to glue these together more elegantly with monads +