Fork me on GitHub
#code-reviews
<
2019-08-19
>
delaguardo11:08:01

It is not working as expected if you have nil as an element in your tree

delaguardo11:08:03

You can check on each iteration of loop that incoming tree is not nil

(defn my-own-flatten [tree]
  (loop [[h & t :as tr] tree
         result []]
    (cond
      (nil? tr) result
      (coll? h) (recur t (doall (concat result (my-own-flatten h))))
      :else (recur t (doall (concat result [h]))))))

delaguardo11:08:09

something like that

delaguardo11:08:33

also because you are using coll? it will unwrap and flattenize maps same way as sequences. It is ok if this is intentional, but not the same behavior as flatten from core

tdantas16:08:54

yeah @delaguardo agree. instead of coll? better to use sequential? right ?

delaguardo16:08:46

yes, sequential? is a better option here

tdantas16:08:39

thx for the nil heads up

tdantas16:08:54

do you have a better flatten implementation ?

delaguardo16:08:22

from the top of my head

(defn flatten* [obj]
  (if (sequential? obj)
    (lazy-cat
     (flatten* (first obj))
     (when-let [r (not-empty (rest obj))]
       (flatten* (rest obj))))
    (list obj)))

noisesmith20:08:34

to my eye the if and the when-let are doing the same thing, and one could be removed(?)

delaguardo16:08:35

there is also tree-seq function which should work better in that case

Chase20:08:53

for those who don't follow that to the #clojure thread I'll reiterate that I've found exercism very, very helpful in getting over some learning humps.

Chase20:08:50

So I thank all the mentors and those considering it. It's been so satisfying to solve a problem and see those tests pass. Then also refactoring with mentor advice or I also head to #beginners for more advice.

Chase20:08:48

Speaking of the tests, that has shown me the benefit of tests in a much more real way and I think that is something I will take with me into my programming career. I know now they can help you build to your solution, not just test solutions after the fact.

Chase20:08:36

One last bit and I'll leave you be. I also check the community solutions after solving a problem and a couple times my solution has matched exactly the elegant solution that has received the most stars from other users! Besides just learning from all the solutions, that has been such a huge motivator to see as I struggle so hard at programming yet get a little bit of evidence I can do this and come up with elegant solutions at the same time! So yeah, I'm a big fan of exercism. If you can spare the time, us learners are greatly appreciative of the mentorship!

noisesmith20:08:30

you've probably heard of http://4clojure.com - but that's worth looking at if not

Chase20:08:51

right?! It exposes you to real world usage of all the cool core functions of Clojure and I think it sinks in better because you've just solved that problem yourself so it's easier to understand these new function uses imo.

noisesmith20:08:51

(it is stuck on an old clojure version, but there's a lot to learn from)

Chase20:08:35

i'm a fan of 4clojure too for sure! case in point, I just solved a problem on there and low and behold my solution matched how @mfikes solved it and he is one of my clojure idols. I also see 3 other great ways to solve it. I love it!

noisesmith20:08:03

I bet exercism has less code-golf answers (writing bad code just to get a smaller character count)

Chase20:08:22

are you on there @noisesmith? I'll follow you to compare with your answers. You frequently help me a lot on the beginner channel. Any others you recommend to follow?

Chase20:08:39

found you!

Chase20:08:12

you still have 12 more solutions to go buddy! get to it! lol, just kidding, just kidding.

noisesmith20:08:16

haha, I haven't updated anything in a while, and those last problems are the hard ones :D