Fork me on GitHub
Vlad Kryvokoniev12:08:26

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))


use update instead of assoc

Vlad Kryvokoniev12:08:34

can u explain why please?


user=> (def a (atom {:b [1]}))
user=> @a
{:b [1]}
user=> (swap! a update :b conj 2)
{:b [1 2]}


Because it’s not atomic if you do two atom operations- @ and swap!


That’s a race condition


With a single op it’s atomic

Vlad Kryvokoniev13:08:56

@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

Emi Gal17:08:39

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: