Hacker News new | ask | show | jobs
by throwawaw 2131 days ago
Fun seeing that pretty much everyone else finds that idiom confusing too. Half-serious, over breakfast:

    (case [(> n min) (< n max)]
      [true true] n
      [true false] max
      [false true] min)
(Side note: Clojure's `>` and `<` are kind of unreadable to begin with. Turning `if (> n min)` into "if n is greater than min" takes some work for me, still, after more than a year.)
4 comments

However, prefix notation becomes much easier to read and reason about when you have more than two things to compare:

    (> 3 2 1 ...)
vs

    (3 > 2) && (2 > 1) && ...
Alternatively, since min and max are part of clojure.core:

   (-> n
       (max vmin)
       (min vmax))
> (Side note: Clojure's `>` and `<` are kind of unreadable to begin with.

I felt the same for awhile but now I just mentally put the operator between the operands, so (> 3 2) is the same as 3 > 2.

What about the [false false] case where min ≥ n ≥ max?