Skip to content

Instantly share code, notes, and snippets.

@pwim
Created May 28, 2012 03:00
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 pwim/2816967 to your computer and use it in GitHub Desktop.
Save pwim/2816967 to your computer and use it in GitHub Desktop.
map + find
# Is there already a pattern for doing this?
def map_find(enumerable)
enumerable.each do |e|
t = yield(e)
return t if t
end
nil
end
# equivalent to the following but with one less function call
t = enumerable.find {|e| foo(e)}
foo(t) if t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment