Created
June 26, 2013 04:23
-
-
Save fabioyamate/5864768 to your computer and use it in GitHub Desktop.
reduce application
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
# the common thinking when you are transforming some data to a non-nil value is use map | |
a = { foo: 'bar', bar: 'baz' } | |
[:foo, :bar, :baz, :foobar].map { |k| a[k] } | |
# => ['bar', 'baz', nil, nil] | |
# to drop nil cases | |
[:foo, :bar, :baz, :foobar].map { |k| a[k] }.compact | |
# => ['bar', 'baz'] | |
[:foo, :bar, :baz, :foobar].reduce([]) do |l, k| | |
l << a[k] if a.has_key?(k) | |
l | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment