From: matthew@... Date: 2018-12-17T03:43:13+00:00 Subject: [ruby-core:90582] [Ruby trunk Feature#15323] [PATCH] Proposal: Add Enumerable#filter_map Issue #15323 has been updated by phluid61 (Matthew Kerwin). tny (Tony Sunny) wrote: > Could't we use reduce for this? > ~~~ ruby > (1..10).reduce([]) { |a, i| i.even? ? a << (i * 2) : a } > ~~~ Yep, that's mentioned in the original ticket too. There's also #each_with_object that lets you write the block almost the same as in the proposal: ~~~ruby (1..10).each_with_object([]) { |i, a| a << i * 2 if i.even? } ~~~ The big difference here is you can capture nil/false values, because the filter test is explicitly separated from the map operation. ---------------------------------------- Feature #15323: [PATCH] Proposal: Add Enumerable#filter_map https://bugs.ruby-lang.org/issues/15323#change-75739 * Author: alfonsojimenez (Alfonso Jim��nez) * Status: Open * 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: