Created
February 29, 2016 19:25
-
-
Save mostr/372f65ece0ad6ea218d1 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
(ns core | |
(:require [mount.core :as mount] | |
[user-finder :refer [user-finder user-finder-2]] | |
[users-db :refer [db])) | |
(mount/start) | |
(user-finder 2) ; finds user with id = 2 | |
; when I stop users-db it still works (althought db state is stopped) | |
; this is because state is partially applied fn that resolves db during state's start I guess | |
(mount/stop #'users-db/db) | |
(user-finder 2) ; finds user with id = 2 | |
; if there is no partial application it works as expected, but requires db state | |
(user-finder-2 db 2) ; throws ex that there is no db state | |
; is there a way to have benefits of this partially applied version (no need for passing db everywhere) | |
; as well as having behavior of the seconduser-finder-2 - have "live-link" to db state? | |
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
(ns finders) | |
(defn find-user [users id] | |
(some #(if (= (:id %) id) %) users)) | |
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
(ns user-finder | |
(:require [mount.core :refer [defstate]] | |
[finders :as finders] | |
[users-db :refer [db]])) | |
(defstate user-finder :start (partial finders/find-user db)) | |
(defstate user-finder-2 :start finders/find-user) |
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
(ns users-db | |
(:require [mount.core :refer [defstate]])) | |
(def users #{{:id 1 :name "foo"} | |
{:id 2 :name "bar"}}) | |
(defstate db :start users) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment