Fork me on GitHub
#clojurescript
<
2020-12-15
>
Sophie08:12:31

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?

3
lukas.rychtecky09:12:41

1. I would say that :qm is string not a number

p-himik09:12:54

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.

lukas.rychtecky09:12:55

2. The code is too complex IMO, you can simplify it like this:

(reduce (fn [acc [_ {:keys [qm]}]] (+ acc qm)) 0 @zimmer)

p-himik09:12:25

@U6HHQ05GB Your code is incorrect. You're missing something that in the original code was [_ %2].

p-himik09:12:09

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.

p-himik09:12:33

(should also be marginally faster that way)

lukas.rychtecky09:12:47

I don’t think so, because of [_ {:keys [qm]}]

p-himik09:12:04

Oh, sorry - you're right! I just woke up and read it wrong three times in a row.

😄 3
lukas.rychtecky09:12:19

Be honest I didn’t try it in REPL , I wrote it just the top of my head 😄

Sophie09:12:51

Thank you Guys for your suggestions. I will report my progress.

Sophie10:12:38

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 @U2FRKM4TW. Thank you so much

👍 3
Calum Boal13:12:47

Anyone tried using grpc-web in clojurescript?

leif18:12:57

Is there any way to set different classpath values for different compile targets using the standard clojurescript build tools?

leif18:12:46

(Sort of like different profiles in lein.)

dnolen18:12:20

you can do that w/ tools deps yes

dnolen18:12:54

nothing specific to ClojureScript here, looks the same as if you needed this for Clojure

leif18:12:24

@dnolen Oh cool, thanks. Are you referring to the :extra-paths key in https://clojure.org/reference/deps_and_cli#_make_classpath_modifiers ?

dnolen18:12:44

and those can be different for each alias

leif18:12:01

Makes sense. Seems like it would go something like:

{...
 :aliases {:package {:main-opts ...
                     :extra-paths ...}}
          ...}}
?

leif18:12:04

Anyway, thanks.