Created
May 23, 2014 17:32
-
-
Save fortruce/b040885fb49a3a643623 to your computer and use it in GitHub Desktop.
Clojure Removing Dependencies on Global State
This file contains hidden or 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
| ;; This is a weird LightTable REPL session trying to understand how to actually use the concepts outlined | |
| ;; in http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded and the related talk he gave at | |
| ;; Clojure West. | |
| ;; The following code does NOT make sense when run in order as I was evaluating things on the fly with LightTable. | |
| (defn system [] | |
| {:db {:conn nil | |
| :collection "collection"}}) | |
| (defn start [system] | |
| (-> system | |
| (assoc-in [:db :conn] "conn"))) | |
| (defn mock [] | |
| {:db {:conn nil | |
| :collection "mock"}}) | |
| (defn stop [system] | |
| (-> system | |
| (assoc-in [:db :conn] nil))) | |
| (def sys nil) | |
| (alter-var-root #'system/sys (constantly (start (system)))) | |
| (alter-var-root #'system/sys (constantly (start (mock)))) | |
| (defn get-shout [db] | |
| (case (:collection db) | |
| "collection" "normal shout" | |
| "mock" "mocked shout" | |
| "no collection ERROR")) | |
| sys | |
| (defn init-db [db] | |
| (alter-var-root #'get-shout (constantly (partial get-shout db)))) | |
| (init-db (:db (start (system)))) | |
| (get-shout) | |
| (init-db (:db (start (mock)))) | |
| (let [db (:db sys)] | |
| (get-shout db)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment