This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-06
Channels
- # adventofcode (71)
- # aleph (1)
- # announcements (6)
- # aws (1)
- # babashka (27)
- # beginners (60)
- # biff (7)
- # calva (3)
- # clj-kondo (3)
- # clj-yaml (1)
- # clojure (60)
- # clojure-europe (43)
- # clojure-nl (3)
- # clojure-norway (75)
- # clojurescript (16)
- # code-reviews (7)
- # css (4)
- # cursive (47)
- # datascript (4)
- # events (5)
- # fulcro (37)
- # gratitude (5)
- # hyperfiddle (4)
- # introduce-yourself (4)
- # joyride (23)
- # juxt (4)
- # malli (4)
- # membrane (64)
- # nbb (8)
- # off-topic (12)
- # other-languages (6)
- # pathom (6)
- # polylith (9)
- # random (3)
- # rdf (66)
- # reitit (3)
- # releases (2)
- # shadow-cljs (18)
- # tree-sitter (10)
Hi guys 🙂 I’m doing advent of code challenges with clojure (learning it right now) and got stuck on something really stupid. I’ve got the following code for the 5th day parsing the first part of the input (with the containers). The result from the last map is seq of those maps. {:curr-el 10, :4 [], :7 [\V], :1 [], :8 [], :9 [\L], :2 [], :5 [\M], :3 [], :6 []} {:curr-el 10, :4 [], :7 [\G], :1 [\G], :8 [], :9 [\D], :2 [], :5 [\V], :3 [], :6 [\C]} {:curr-el 10, :4 [], :7 [\Z], :1 [\J], :8 [\C], :9 [\J], :2 [], :5 [\Q], :3 [], :6 [\W]} {:curr-el 10, :4 [\W], :7 [\D], :1 [\W], :8 [\G], :9 [\C], :2 [], :5 [\G], :3 [], :6 [\V]} {:curr-el 10, :4 [\N], :7 [\C], :1 [\R], :8 [\M], :9 [\W], :2 [], :5 [\B], :3 [\G], :6 [\D]} {:curr-el 10, :4 [\C], :7 [\N], :1 [\F], :8 [\N], :9 [\N], :2 [\M], :5 [\S], :3 [\H], :6 [\T]} {:curr-el 10, :4 [\R], :7 [\B], :1 [\T], :8 [\J], :9 [\P], :2 [\W], :5 [\F], :3 [\N], :6 [\R]} {:curr-el 10, :4 [\J], :7 [\H], :1 [\Z], :8 [\S], :9 [\G], :2 [\G], :5 [\W], :3 [\J], :6 [\S]} Then I’m trying to merge them into one map, but first remove the curr-el, cause I don;t need it anymore. Unfortunately the acc here is nil and I’m not really sure why. As far as i Know reduce accepts a fn that accepts [accumulator, current-element] The current element is one of those maps, but the acc is nil. Another part of the question is should I do something else in case that I want to append the values of those vectors in the end? Btw I tried merge-with conj/into instead of this reduce but it didin’t worked at all also
(->> (str/split containers #"\n")
rest
(map mapper)
(map (partial reduce create-supply {:1 [] :2 [] :3 [] :4 [] :5 [] :6 [] :7 [] :8 [] :9 [] :curr-el 1}))
(reduce (fn [acc el]
(let [removed (apply dissoc el [:curr-el])
result (merge (doto removed prn) (doto acc prn))]))
{:1 [] :2 [] :3 [] :4 [] :5 [] :6 [] :7 [] :8 [] :9 []}))
There's an #C0GLTDB2T channel where you may find other folks working on these challenges...
You don't return anything from the let
in that reducing function. The form is therefore evaluating to nil
.
@U0ETXRFEW that’s correct, but the acc is also null when enters the function ((doto acc prn) prints nil )
@U04V70XH6 thank you, I’ll join it, sorry for the spam now 😕
Not an expert, but you are not providing an init value for acc
which means you would enter this case of the docs for reduce
:
>
f should be a function of 2 arguments. If val is not supplied,
> returns the result of applying f to the first 2 items in coll, then
> applying f to that result and the 3rd item, etc.
If f
returns nil
regardless of arguments, then maybe what you see is expected. There's a reductions
function that can help in some cases to trace what's going on (not sure if useful in this case).Actually it seems like the function should get passed something non-nil first...:
(reductions #() {:1 [] :2 [] :3 [] :4 [] :5 [] :6 [] :7 [] :8 [] :9 []})
=> ([:4 []] () () () () () () () ())
Interestingly:
(reductions #() {:1 [] :2 [] :3 [] :4 []})
=> ([:1 []] () () ())
I think there is a lesson for me in here. 😃