-
-
Save havenwood/24a6a9fa42980d9489f6 to your computer and use it in GitHub Desktop.
An example #filter_map in Ruby
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
module Enumerable | |
def filter_map filter, mapper | |
each_with_object([]) do |item, result| | |
result << mapper.call(item) if filter.call(item) | |
end | |
end | |
end | |
[1, 2, 3, 4].filter_map :even?.to_proc, :to_s.to_proc | |
#=> ["2", "4"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment