It is not working as expected if you have nil
as an element in your tree
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]))))))
something like that
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
yeah @delaguardo agree. instead of coll? better to use sequential?
right ?
yes, sequential?
is a better option here
thx
thx for the nil heads up
do you have a better flatten implementation ?
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)))
there is also tree-seq
function which should work better in that case
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.
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.
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.
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!
yeah, the community solutions are super helpful. seeing people solve it different ways, with different clojure features, really helps my brain acclimate to different ways of thinking
you've probably heard of http://4clojure.com - but that's worth looking at if not
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.
(it is stuck on an old clojure version, but there's a lot to learn from)
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!
I bet exercism has less code-golf answers (writing bad code just to get a smaller character count)
to my eye the if and the when-let are doing the same thing, and one could be removed(?)
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?
found you!
you still have 12 more solutions to go buddy! get to it! lol, just kidding, just kidding.
haha, I haven't updated anything in a while, and those last problems are the hard ones :D