Fork me on GitHub
#beginners
<
2020-08-27
>
Endre Bakken Stovner07:08:54

I have a question about this let:

(let [response (try
  (msg/save-message! ?data)
  (assoc ?data :timestamp (java.util.Date.))
  (catch Exception e
  (let [{id :guestbook/error-id errors :errors} (ex-data e)]
    (case id :validation {:errors errors} ;;else
          {:errors
          {:server-error ["Failed to save message!"]}}))))]
Isn't the third line (assoc data? :timestamp ...) effectively a no-op? It just associates the timestamp and then discards the map...

kimim07:08:06

Whether data? is defined as an atom elsewhare?

Endre Bakken Stovner07:08:40

Good question! But then one would need to use swap!, no?

Endre Bakken Stovner07:08:57

(defmethod handle-message :message/create!
  [{:keys [?data uid] :as message}]
   ... here the let begins

rpkarlsson08:08:46

Doesn't seem to be a noop:

(let [?data {}
      response (try
                 #_(msg/save-message! ?data)
                 (assoc ?data :timestamp (java.util.Date.))
                 (catch Exception e))]
  response)
;; => {:timestamp #inst "2020-08-27T08:03:11.462-00:00"}
Simplified the expression. But the assoc returns the map and it gets bound to the response symbol.

Endre Bakken Stovner08:08:27

You are correct. It first saves the message and then associates the timestamp in the map which becomes the response.

PB17:08:41

Riddle me this:

(apply + '(95 85 734.45 99.6 15 33.71))
1062.7600000000002

dpsutton17:08:03

common surprise when first seen. tl:dr; can't represent all base 10 values in base 2 exactly so sometimes you get these seemingly strange results

PB17:08:51

That makes sense, thanks

👍 3
dpsutton17:08:49

i think floating point addition isn't commutative either although i can't remember the examples. !(a + b = b + a) for all a,b in the floating point reals)

jumar09:08:03

Associativity isn't guaranteed for floating points (in IEEE754 representation):

(+ 3.14 (+ 10e20 -10e20) )
3.14

(+ (+ 3.14 10e20) -10e20)
0.0

dpsutton12:08:48

Ah thank you!

andy.fingerhut19:08:32

Some more details on floating point number representation and their properties are here, including a link to a longer article "What every computer scientist should know about floating point point arithmetic" (although skimming that article now, I suspect not every computer scientist really needs to know everything in that article -- some don't use floating point arithmetic in their work much at all): https://clojure.org/guides/equality#_floating_point_numbers_are_usually_approximations

sogaiu09:08:06

i presume you don't need any of the following, but i was helped recently by: https://github.com/bartaz/ieee754-visualization i found the associated presentation / talk to be better than a number of other things i had come across.

👍 3
jumar09:08:36

It might not be obvious from the clojure docs but BigDecimal suffers from the same issues as double when trying to represent certain numbers; it's just that the BigDecimal can use all available memory if needed. The gotcha (sort of implicitly mentioned in the docs) can be see here:

(bigdec 1/3)
Execution error (ArithmeticException) at java.math.BigDecimal/divide (BigDecimal.java:1723).
Non-terminating decimal expansion; no exact representable decimal result.

eval-on-point22:08:37

I'm a little stumped by reagent at the moment. Why doesn't the following snippet trigger an update of my component?

(def data (r/atom nil))

(dom/render [some-component @data] dom-node)

(reset! data "new data")

dpsutton22:08:42

Because your component doesn’t deref the atom it just receives a value. If you pass it in and deref there it should work

eval-on-point22:08:45

If some-component is a form-3, then how would I accomplish that? For example,

eval-on-point22:08:32

(defn some-component [g]
  (r/create-class
    {:reagent-render
     (fn [g] (js/console.log "rendered") [:div])

     :component-did-mount
     (fn [this] (js/console.log "mounted"))

     :component-did-update
     #(js/console.log "updated")

     :component-will-unmount
     #(js/console.log "unmounting")}))

eval-on-point22:08:39

derefing g in the render and mount methods don't seem to work, and neither does swapping g into a component-local atom

dpsutton23:08:40

Deref the argument of the render function

dpsutton23:08:23

G is a ratom and deref in the render function

dpsutton23:08:46

Sorry on phone and missed what you said. I’ll check when I’m home

eval-on-point23:08:19

WOW thank you so much. Could've sworn I had tried that early on but must not have. It works now. I really appreciate the help with this

👍 3