Hi, I'm trying to sum up values from a nested map. But instead of simply summing up the values my function builds a sequence of all steps. The outcome should be 96 but it is 10505267698496. This is the Code:
(rf/reg-sub
:sum-up-roomsize
(fn []
(let [buffer (atom 0)]
(let [zimmer (rf/subscribe [:entry-point-rooms])]
(doall
(for [[_ %2] @zimmer]
(swap! buffer + (%2 :qm))
)
)
)
)
)
)
Any suggestions what's going wrong?
And is there perhaps a more elegant solution?1. I would say that :qm
is string not a number
Your function returns the result of doall
. Which realizes and returns the sequence created by for
. Sequence, where each item is the result of swap!
. And swap!
returns the value that was swapped in.
I don't know how your code returns 10505267698496
- it should return a sequence.
You can replace doall
with something like this:
(apply + (map #(:qm (second %)) @zimmer))
Or you can use reduce
.
Now, for some nitpicks:
- You should read this: https://day8.github.io/re-frame/subscriptions/#reg-sub
In particular, pay attention to the description and the examples of "Layer 3".
In short, the way you use subscribe
within reg-sub
is not ideal. Not a big deal, but creates code that will be harder to support later.
- Try not to start variable names with %
- otherwise, you gonna have a bad time dealing with lambdas.
- The signature of your sub function is wrong - it has to be a function of two arguments. It will work but it's not robust.2. The code is too complex IMO, you can simplify it like this:
(reduce (fn [acc [_ {:keys [qm]}]] (+ acc qm)) 0 @zimmer)
@lukas.rychtecky Your code is incorrect. You're missing something that in the original code was [_ %2]
.
apply
above is the shortest solution. Could be made shorter if we knew that @zimmer
is a map - then second
could be replaced with val
.
(should also be marginally faster that way)
I donβt think so, because of [_ {:keys [qm]}]
Oh, sorry - you're right! I just woke up and read it wrong three times in a row.
Be honest I didnβt try it in REPL , I wrote it just the top of my head π
Thank you Guys for your suggestions. I will report my progress.
Nice, I have got two working solutions π.
(rf/reg-sub
:sum-up-roomsize
(fn []
(let [zimmer (rf/subscribe [:entry-point-rooms])]
(reduce (fn [acc [_ {:keys [qm]}]] (+ acc qm)) 0 @zimmer)
)
)
)
and second:
(rf/reg-sub
:sum-up-roomsize
(fn []
(let [zimmer (rf/subscribe [:entry-point-rooms])]
(apply + (map #(:qm (second %)) @zimmer))
)
)
)
and I can improve my skills with your hints @p-himik.
Thank you so muchAnyone tried using grpc-web in clojurescript?
Is there any way to set different classpath values for different compile targets using the standard clojurescript build tools?
(Sort of like different profiles in lein.)
you can do that w/ tools deps yes
nothing specific to ClojureScript here, looks the same as if you needed this for Clojure
@dnolen Oh cool, thanks. Are you referring to the :extra-paths
key in https://clojure.org/reference/deps_and_cli#_make_classpath_modifiers ?
and those can be different for each alias
Makes sense. Seems like it would go something like:
{...
:aliases {:package {:main-opts ...
:extra-paths ...}}
...}}
?Anyway, thanks.
yep