Reputation: 5512
I'm experiencing unexpected evaluation of the identical (do ...)
form when evaluated in a function vs being evaluated in REPL standalone. Here's the first example:
;Example1
(defn example [req]
(do
(println (-> req :body :text))
(create-message! (-> req :body :text))
(println (map :text (get-messages)))
)
)
(example {:body {:text "text1"}})
Output of the println will be the following for example1:
text1
()
Here's the 2nd example:
;Example2
(def req {:body {:text "text1"}})
(do
(println (-> req :body :text))
(create-message! (-> req :body :text))
(println (map :text (get-messages)))
)
Output here is:
text1
(text1)
Function create-message! prepends a message to an atom list. Function get-messages returns the atom list.
In the 1st example it seems that result of the 2nd println is an empty list, instead of the expected one-element list containing "text1". The atom list does get updated as expected in both examples. It seems as if get-messages get evaluated before create-message! in 1st example (or something else unexpected is happening).
What is happening here and why?
EDIT
Here's the code for create-message and get-messages
(defn get-messages
([] (get-messages 10))
([length] (get-messages length (jt/instant)))
([length before] (take length (filter #(jt/before? (:timestamp %) before) @domain/messages)))
)
(defn create-message!
[text]
(swap! domain/messages #(cons (domain/->Message (jt/instant) text) %))
)
Here's the definition of the messages atom in domain namespace:
(ns chatovic.domain)
(defrecord Message [timestamp text])
(def messages (atom []))
Upvotes: 0
Views: 55
Reputation: 13185
It seems that your code uses (jt/instant)
to filter messages returned from your atom and this might lead to different results between invocations due to time sensitivity.
Upvotes: 0