This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-08-27
Channels
- # admin-announcements (1)
- # announcements (11)
- # babashka (17)
- # beginners (26)
- # calva (6)
- # cider (2)
- # circleci (1)
- # clojure (41)
- # clojure-dev (1)
- # clojure-europe (31)
- # clojure-france (2)
- # clojure-italy (10)
- # clojure-nl (7)
- # clojure-norway (5)
- # clojure-spec (15)
- # clojure-uk (42)
- # clojurescript (4)
- # code-reviews (12)
- # conjure (10)
- # datalog (2)
- # datascript (15)
- # datomic (37)
- # emacs (1)
- # events (5)
- # fulcro (19)
- # jobs (1)
- # jobs-discuss (9)
- # kaocha (2)
- # luminus (14)
- # meander (4)
- # membrane (39)
- # off-topic (26)
- # other-languages (2)
- # re-frame (13)
- # reitit (6)
- # rewrite-clj (39)
- # sci (6)
- # shadow-cljs (33)
- # test-check (15)
- # vrac (17)
- # xtdb (7)
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...Good question! But then one would need to use swap!, no?
(defmethod handle-message :message/create!
[{:keys [?data uid] :as message}]
... here the let begins
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.You are correct. It first saves the message and then associates the timestamp in the map which becomes the response.
(+ 0.1 0.2)
and see https://0.30000000000000004.com/
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
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)
Associativity isn't guaranteed for floating points (in IEEE754 representation):
(+ 3.14 (+ 10e20 -10e20) )
3.14
(+ (+ 3.14 10e20) -10e20)
0.0
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
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.
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.
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")
Because your component doesn’t deref the atom it just receives a value. If you pass it in and deref there it should work
If some-component
is a form-3, then how would I accomplish that? For example,
(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")}))
derefing g
in the render and mount methods don't seem to work, and neither does swapping g
into a component-local atom
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