Created
April 4, 2015 21:22
-
-
Save jsanders/ea264d018bf945f87e81 to your computer and use it in GitHub Desktop.
Functional min_by
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def min_by(ary, fn) | |
first, *rest = ary; | |
min_elem, _ = rest.reduce([first, fn.call(first)]) do |(min_elem, min), new_elem| | |
if (new_min = f.call(new_elem)) < min | |
[ new_elem, new_min ] | |
else | |
[ min_elem, min ] | |
end | |
end | |
min_elem | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment