This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-21
Channels
- # babashka (11)
- # beginners (15)
- # cider (2)
- # cljdoc (36)
- # cljsrn (3)
- # clojure (6)
- # clojure-europe (9)
- # clojurescript (17)
- # conjure (3)
- # deps-new (6)
- # development-containers (1)
- # emacs (3)
- # fulcro (4)
- # gratitude (14)
- # introduce-yourself (2)
- # joker (14)
- # lsp (1)
- # luminus (7)
- # malli (6)
- # polylith (7)
- # releases (1)
- # reveal (3)
- # sql (4)
- # vim (18)
Hi, Do you guys have an idea how to make it more concise, readable?
(swap! app-state assoc :twitch-clips (conj (:twitch-clips @app-state) clip-link))
can u explain why please?
user=> (def a (atom {:b [1]}))
#'user/a
user=> @a
{:b [1]}
user=> (swap! a update :b conj 2)
{:b [1 2]}
@U1Z392WMQ thanks for explanation. @U050ECB92 do you mean I should remove the dereference character from there?
Yes. You’re deferencing and swapping - and swap always passes the derefenced value to the function passed in
gotcha, thanks
I’ve done a bit of googling but couldn’t find much useful info: what are the advantages / disadvantages of using update
and update-in
vs assoc
and assoc-in
to update data in a map?
Just a different interface for a similar behavior. update takes a function of the current value, assoc takes a new value to replace the existing one with
Given a map m,
(update m :clicks inc)
is equivalent to
(assoc m :clicks (inc (:clicks m)))
Both are correct but the former is arguably easier to read
My rule of thumb is: if the new value depends on the old value (like didibus said), update
probably makes more sense, otherwise use assoc
You need get
/ get-in
to find the value you want to assoc
if you intend to call a function on it before assoc
ing it into the updated map. update
makes this slightly less verbose.
Under the hood it's just get
and assoc
:
https://github.com/clojure/clojure/blob/clojure-1.10.1/src/clj/clojure/core.clj#L6188