clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
Sophie 2020-12-15T08:58:31.325900Z

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
lukas.rychtecky 2020-12-15T09:11:41.326Z

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

p-himik 2020-12-15T09:11:54.326200Z

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.rychtecky 2020-12-15T09:14:55.326500Z

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

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

p-himik 2020-12-15T09:16:25.326700Z

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

p-himik 2020-12-15T09:17:09.326900Z

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-himik 2020-12-15T09:17:33.327100Z

(should also be marginally faster that way)

lukas.rychtecky 2020-12-15T09:17:47.327300Z

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

p-himik 2020-12-15T09:18:04.327500Z

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

πŸ˜„ 1
lukas.rychtecky 2020-12-15T09:18:19.327700Z

Be honest I didn’t try it in REPL , I wrote it just the top of my head πŸ˜„

Sophie 2020-12-15T09:28:51.328200Z

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

Sophie 2020-12-15T10:00:38.328700Z

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 much

πŸ‘ 1
Calum Boal 2020-12-15T13:40:47.329800Z

Anyone tried using grpc-web in clojurescript?

2020-12-15T18:39:57.330700Z

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

2020-12-15T18:40:46.331300Z

(Sort of like different profiles in lein.)

dnolen 2020-12-15T18:45:20.331600Z

you can do that w/ tools deps yes

dnolen 2020-12-15T18:45:54.332200Z

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

2020-12-15T18:49:24.332900Z

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

dnolen 2020-12-15T18:49:44.333100Z

and those can be different for each alias

2020-12-15T18:54:01.334800Z

Makes sense. Seems like it would go something like:

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

2020-12-15T18:54:04.335Z

Anyway, thanks.

alexmiller 2020-12-15T18:54:20.335200Z

yep