[ruby-core:95283] [Ruby master Feature#15323] [PATCH] Proposal: Add Enumerable#filter_map

From: eregontp@...
Date: 2019-10-08 21:35:32 UTC
List: ruby-core #95283
Issue #15323 has been updated by Eregon (Benoit Daloze).


jonathanhefner (Jonathan Hefner) wrote:
> Eregon (Benoit Daloze) wrote:
> I would expect to get a value for every valid record, no matter what `field` is.

What if `record.send(field)` returns `nil`?
Then the only way is:
```ruby
records.filter { |record| record.valid? }.map { |record| record.send(field) }
```

So this kind of issue is intrinsically there for `filter_map`.
`filter_map` is a footgun if `nil` can be returned.

I see the point that `nil` is like "missing element, filter it out" versus `false` being a regular value (and `true` too).

I tend to agree with you now, I think just filtering out `nil` values would be better than also removing `false`.

----------------------------------------
Feature #15323: [PATCH] Proposal: Add Enumerable#filter_map
https://bugs.ruby-lang.org/issues/15323#change-81961

* Author: alfonsojimenez (Alfonso Jim駭ez)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
This is a proposal for a combined `filter` + `map` method (https://bugs.ruby-lang.org/issues/5663).

This method both filters and maps the elements of an enumerable in just one iteration:

~~~ ruby
(1..10).filter_map { |i| i * 2 if i.even? } #=> [4, 8, 12, 16, 20]
~~~

GitHub PR: https://github.com/ruby/ruby/pull/2017


 

---Files--------------------------------
0001-Adding-Enumerable-filter_map.patch (4.61 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next