code-reviews

Georgi Stoyanov 2022-12-06T21:35:51.804699Z

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 []}))

๐Ÿ‘‹ 2
seancorfield 2022-12-06T22:28:04.446839Z

There's an #adventofcode channel where you may find other folks working on these challenges...

๐Ÿ™Œ 1
pez 2022-12-06T22:31:47.815959Z

You don't return anything from the let in that reducing function. The form is therefore evaluating to nil.

๐Ÿ‘๐Ÿป 1
Georgi Stoyanov 2022-12-07T07:55:12.075609Z

@pez thatโ€™s correct, but the acc is also null when enters the function ((doto acc prn) prints nil )

Georgi Stoyanov 2022-12-07T07:55:25.482269Z

@seancorfield thank you, Iโ€™ll join it, sorry for the spam now ๐Ÿ˜•

pez 2022-12-07T08:04:12.650759Z

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

pez 2022-12-07T09:25:30.600479Z

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. ๐Ÿ˜ƒ

๐Ÿ‘ 1