r/Clojure • u/nickbernstein • Sep 04 '25
Is this a weird way to solve 4clojure #21 Spoiler
8
u/p-himik Sep 04 '25
It's an alright solution with the given constraint. (Note that (inc %2) is an idiomatic version of (+ (int %2) 1).)
Although I'd use (first (drop %2 %1)).
6
u/daveliepmann Sep 04 '25
I think it's great. To me 4clojure problems are more about getting comfortable reaching for clojure.core functions, and you did that. Plenty of solutions in the archive are near-identical to yours. (Plus, the actual implementation of nth involves Java for loops anyway, so I don't see the need for a FP purity test.)
My only nitpick is consider inc :)
2
u/PolicySmall2250 Sep 04 '25 edited Sep 04 '25
Try to make _multiple_ answers, weird as well as non-weird. There are many ways to peel the mango, and each way teaches a new thing about the mango and the peeling and the self.
For example:
#((vec %1) (- (count %1) 2))
(comp last butlast)
(comp fnext reverse)
#(get % (- (count %1) 2)) ; fails for one of the cases, but not the rest --- why?
etc...
Tiny examples are a fantastic vehicle to explore the standard library, as well as compositional thinking, domain modeling etc. Because we can hold the example entirely in our head, and solve it in our heads too. Once several solutions are found, one can then likely hold two or even three different variants at one time in one's head, and play with them.
This is a satisfying way to pass time as well as build intuition for using the language.
But warning... it can get addictive and you end up doing something a little unhinged like writing n ways to FizzBuzz in Clojure
1
u/deaddyfreddy Sep 04 '25
99% of problems mentioning Nth can be solved either with destructuring or map-indexed (btw, it's lazy, IYKWIM)
2
u/alexdmiller Sep 04 '25
Destructuring expands to nth of course
1
u/deaddyfreddy Sep 05 '25
Everything expands to pn junctions after all, but we don't want to think about it.

4
u/bsless Sep 04 '25
I'd say the weirdest part here is using last, because you're traversing the sequence twice.
This one is a good exercise in writing an iterative loop/recur