-
-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: encourage Enumerable#filter_map
#178
Comments
ghiculescu
changed the title
Add
Feature request: encourage Oct 8, 2020
Enumerable#filter_map
Enumerable#filter_map
This is a note about an incompatibility between [true, false, nil].compact #=> [true, false]
[true, false, nil].filter_map(&:itself) #=> [true] |
koic
added a commit
to koic/rubocop-performance
that referenced
this issue
Feb 9, 2021
Fixes rubocop#178. This PR adds new `Performance/FilterMap` cop. In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `select.map` can be replaced by `filter_map`. And it allows `map { ... }.compact` that is not compatible with `filter_map`. ```ruby [true, false, nil].compact #=> [true, false] [true, false, nil].filter_map(&:itself) #=> [true] ``` The following is good and bad cases. ```ruby # bad ary.select(&:foo).map(&:bar) ary.filter(&:foo).map(&:bar) # good ary.filter_map { |o| o.bar if o.foo } ary.map(&:foo).compact # Consider #whether you can safely replace it with `filter_map`. ```
8 tasks
koic
added a commit
to koic/rubocop-performance
that referenced
this issue
Feb 9, 2021
Fixes rubocop#178. This PR adds new `Performance/FilterMap` cop. In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `select.map` can be replaced by `filter_map`. And it allows `map { ... }.compact` that is not compatible with `filter_map`. ```ruby [true, false, nil].compact #=> [true, false] [true, false, nil].filter_map(&:itself) #=> [true] ``` The following is good and bad cases. ```ruby # bad ary.select(&:foo).map(&:bar) ary.filter(&:foo).map(&:bar) # good ary.filter_map { |o| o.bar if o.foo } ary.map(&:foo).compact # Consider whether you can safely replace it with `filter_map`. ```
koic
added a commit
to koic/rubocop-performance
that referenced
this issue
Feb 10, 2021
Fixes rubocop#178. This PR adds new `Performance/FilterMap` cop. In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `select.map` can be replaced by `filter_map`. And it allows `map { ... }.compact` that is not compatible with `filter_map`. ```ruby [true, false, nil].compact #=> [true, false] [true, false, nil].filter_map(&:itself) #=> [true] ``` The following is good and bad cases. ```ruby # bad ary.select(&:foo).map(&:bar) ary.filter(&:foo).map(&:bar) # good ary.filter_map { |o| o.bar if o.foo } ary.map(&:foo).compact # Consider whether you can safely replace it with `filter_map`. ```
koic
added a commit
to koic/rubocop-performance
that referenced
this issue
Feb 10, 2021
Fixes rubocop#178. This PR adds new `Performance/FilterMap` cop. In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `select.map` can be replaced by `filter_map`. And it allows `map { ... }.compact` that is not compatible with `filter_map`. ```ruby [true, false, nil].compact #=> [true, false] [true, false, nil].filter_map(&:itself) #=> [true] ``` The following is good and bad cases. ```ruby # bad ary.select(&:foo).map(&:bar) ary.filter(&:foo).map(&:bar) # good ary.filter_map { |o| o.bar if o.foo } ary.map(&:foo).compact # Consider whether you can safely replace it with `filter_map`. ```
koic
added a commit
to koic/rubocop-performance
that referenced
this issue
Feb 17, 2021
Fixes rubocop#178 and rubocop#193. This PR adds new `Performance/FilterMap` cop. In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `select.map` can be replaced by `filter_map`. And it allows `map { ... }.compact` that is not compatible with `filter_map`. ```ruby [true, false, nil].compact #=> [true, false] [true, false, nil].filter_map(&:itself) #=> [true] ``` The following is good and bad cases. ```ruby # bad ary.select(&:foo).map(&:bar) ary.filter(&:foo).map(&:bar) # good ary.filter_map { |o| o.bar if o.foo } ary.map(&:foo).compact # Consider whether you can safely replace it with `filter_map`. ```
koic
added a commit
to koic/rubocop-performance
that referenced
this issue
Apr 12, 2021
Fixes rubocop#178 and rubocop#193. This PR adds new `Performance/SelectMap` cop. In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `select.map` can be replaced by `filter_map`. The following is good and bad cases. ```ruby # bad ary.select(&:foo).map(&:bar) ary.filter(&:foo).map(&:bar) # good ary.filter_map { |o| o.bar if o.foo } ```
koic
added a commit
to koic/rubocop-performance
that referenced
this issue
Apr 17, 2021
Fixes rubocop#178 and rubocop#193. This PR adds new `Performance/SelectMap` cop. In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `select.map` can be replaced by `filter_map`. The following is good and bad cases. ```ruby # bad ary.select(&:foo).map(&:bar) ary.filter(&:foo).map(&:bar) # good ary.filter_map { |o| o.bar if o.foo } ```
koic
added a commit
that referenced
this issue
Apr 18, 2021
[Fix #178] Add new `Performance/SelectMap` cop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://blog.saeloun.com/2019/05/25/ruby-2-7-enumerable-filter-map.html
This was introduced in ruby 2.7. It adds a modest speedup over
.map {}.compact
or.select {}.map {}
.The text was updated successfully, but these errors were encountered: