Skip to content
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

Closed
ghiculescu opened this issue Oct 8, 2020 · 1 comment · Fixed by #211
Closed

Feature request: encourage Enumerable#filter_map #178

ghiculescu opened this issue Oct 8, 2020 · 1 comment · Fixed by #211

Comments

@ghiculescu
Copy link
Contributor

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 {}.

@ghiculescu ghiculescu changed the title Add Enumerable#filter_map Feature request: encourage Enumerable#filter_map Oct 8, 2020
@koic
Copy link
Member

koic commented Oct 14, 2020

This is a note about an incompatibility between filter_map and compact. filter_map excludes false unlike compact.

[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`.
```
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 koic closed this as completed in #211 Apr 18, 2021
koic added a commit that referenced this issue Apr 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants