r/Clojure • • Dec 01 '24

Advent of Clojure

Advent of Code has started today.

Who is using Clojure to write the solutions? Share the links your repositories!

62 Upvotes

30 comments sorted by

View all comments

Show parent comment

3

u/joinr Dec 02 '24

There's an identity that can simplify the code a little and win points at code golf tournaments:

(if-let [v (freq-second-list %)] v 0)
;;=>
(or (freq-second-list %) 0)
;;=>
(freq-second-list % 0)

If you invoke a hashmap as a function, it's semantically like clojure.core/get, which has an additional arity that accepts a default arg if the key has no associated value.

Particularly I'm having a hard time coming up with efficient algorithms in Clojure.

Just express them the best you can, then figure out how to make them fast or transform to a more efficient algo. More learning opportunity. Eventually you can develop reflexes for faster paths by default (if they are necessary), maybe developing a nice balance of easy and clear / efficient.

1

u/[deleted] Dec 02 '24

That's actually what I was doing intuitively but it's nice to have a confirmation, thank you very much for your advices!