Skip to content

Instantly share code, notes, and snippets.

@jsanders
Created April 4, 2015 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsanders/ea264d018bf945f87e81 to your computer and use it in GitHub Desktop.
Save jsanders/ea264d018bf945f87e81 to your computer and use it in GitHub Desktop.
Functional min_by
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