From: Yura Sokolov <funny.falcon@...> Date: 2012-02-01T13:10:40+09:00 Subject: [ruby-core:42301] [ruby-trunk - Feature #5663] Combined map/select method Issue #5663 has been updated by Yura Sokolov. I often wish to have methods, which likes to `inject` but do use return value of block for next iteration: class Enumerable def accum(container) each{|args| yield container, args} container end end Then I could use it in following ways: instead of enum.inject({}){|h, k| h[k] = true; h} enum.map{|ar| ar.select{|i| i.usefull?}}.flatten(1) enum.map{|i| i + 1 if i.even?}.compact I could enum.accum({}){|h,k| h[k] = true} enum.accum([]){|res, ar| ar.each{|i| res << i if i.usefull?}} enum.accum([]){|res, i| res << i + 1 if i.even?} Well, it is shorter only in case of `inject`, but I still will prefer such method, cause I don't stress GC with many short living arrays. And maybe two common `container`-s could have separate methods: class Enumerable def accum_hash(&block) accum({}, &block) end def accum_ar(&block) accum([], &block) end end enum.accum_hash{|h,k| h[k] = true} enum.accum_array{|res, ar| ar.each{|i| res << i if i.usefull?}} enum.accum_array{|res, i| res << i + 1 if i.even?} ---------------------------------------- Feature #5663: Combined map/select method https://bugs.ruby-lang.org/issues/5663 Author: Yehuda Katz Status: Open Priority: Normal Assignee: Category: lib Target version: 2.0.0 It is pretty common to want to map over an Enumerable, but only include the elements that match a particular filter. A common idiom is: enum.map { |i| i + 1 if i.even? }.compact It is of course also possible to do this with two calls: enum.select { |i| i.even? }.map { |i| i + 1 } Both cases are clumsy and require two iterations through the loop. I'd like to propose a combined method: enum.map_select { |i| i + 1 if i.even? } The only caveat is that it would be impossible to intentionally return nil here; suggestions welcome. The naming is also a strawman; feel free to propose something better. -- http://bugs.ruby-lang.org/