Created
May 28, 2012 03:00
-
-
Save pwim/2816967 to your computer and use it in GitHub Desktop.
map + find
This file contains hidden or 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
# 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