Fork me on GitHub
#code-reviews
<
2022-12-06
>
Georgi Stoyanov21:12:51

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

👋 3
seancorfield22:12:04

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

🙌 1
pez22:12:47

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

1
Georgi Stoyanov07:12:12

@U0ETXRFEW that’s correct, but the acc is also null when enters the function ((doto acc prn) prints nil )

Georgi Stoyanov07:12:25

@U04V70XH6 thank you, I’ll join it, sorry for the spam now 😕

pez08:12:12

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

pez09:12:30

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