Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created July 23, 2015 16:58
Show Gist options
  • Save havenwood/24a6a9fa42980d9489f6 to your computer and use it in GitHub Desktop.
Save havenwood/24a6a9fa42980d9489f6 to your computer and use it in GitHub Desktop.
An example #filter_map in Ruby
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