In clojure you can use reduced to stop before you have done the entire sequence
I was going to say the same! Here's what that looks like:
(reduce (fn [acc x] (+ acc x)) (range 11)) ;; => 55 (reduce (fn [acc x] (if (> x 3) (reduced acc) (+ acc x))) (range 11)) ;; => 6
So we already have that capability built-in (without introducing a new function). Clojure also already has max-by and min-by—max-key, min-key though, perhaps, with a slightly different interface that what you might have expected.
max-by
min-by
I was unaware that this function existed! I'll have to take a look into it to see how it could've helped in a few cases. Thanks for letting me know!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In clojure you can use reduced to stop before you have done the entire sequence
I was going to say the same! Here's what that looks like:
So we already have that capability built-in (without introducing a new function). Clojure also already has
max-by
andmin-by
—max-key, min-key though, perhaps, with a slightly different interface that what you might have expected.I was unaware that this function existed! I'll have to take a look into it to see how it could've helped in a few cases. Thanks for letting me know!