Share your thoughts in the 2025 State of Clojure Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
ago in ClojureScript by
retagged ago by

Hello!

With cljs 1.12.42:

cljs.user=> (max "x" "y")
"y"
cljs.user=>

With cljs 1.12.134:

cljs.user=> (max "x" "y")
"x"
cljs.user=>

Probably related to CLJS-3425 and this commit which added NaN checks:

(defn ^number max
  "Returns the greatest of the nums."
  ([x] x)
  ([x y]
   (cond
     (NaN? x) x
     (NaN? y) y
     (> x y) x
     :else y))
  ([x y & more]
   (reduce max (cljs.core/max x y) more)))

But:

cljs.user=> (NaN? "x")
true
cljs.user=>

Hence the result.

Not sure what the expected result should be since min/max are only supposed to work with numbers but current behavior is surprising.

See also: https://github.com/jank-lang/clojure-test-suite/pull/839.

1 Answer

+2 votes
ago by
selected ago by
 
Best answer

The docstring of max implies that it should be used for numbers.

ago by
Yes. Still don't understand the current behavior. Why not doing something like throwing an error?
ago by
Because checking things can slow things down and in general you don't want this for numeric operators which should be fast
ago by
ok, thank you!
...