I would like to get the smallest element from a vector. For this I use combine the reduce
and min
functions. However, when providing my own implementation of min
I get unexpected results:
user=> (reduce (fn [x y] (< x y) x y) [1 2 3 2 1 0 1 2])
2
user=> (reduce min [1 2 3 2 1 0 1 2 3])
0
The reduce with standard min
returns 0 as expected. However, when I provide my own implementation it returns 2. What am I doing wrong?
You're missing an if
:
(reduce (fn [x y] (if (< x y) x y)) ...)
^-- note the if
works fine. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With