From: "shugo (Shugo Maeda)" Date: 2012-04-13T11:21:53+09:00 Subject: [ruby-core:44325] [ruby-trunk - Feature #5663] Combined map/select method Issue #5663 has been updated by shugo (Shugo Maeda). matz (Yukihiro Matsumoto) wrote: > I am OK with the original map_select behavior, but I don't like the name #map_select. > It is combination of -ect family name with map. > > I prefer the name #filter, but it might be confused by simple alias of select. > Any idea? I think filter should be a simple alias of select. # Otherwise, we can't complain about Scala's collect:p How about filter_map? I've found that Scheme has filter-map. From SRFI-1: filter-map f clist1 clist2 ... -> list Like map, but only true values are saved. (filter-map (lambda (x) (and (number? x) (* x x))) '(a 1 b 3 c 7)) => (1 9 49) The dynamic order in which the various applications of f are made is not specified. At least one of the list arguments must be finite. ---------------------------------------- Feature #5663: Combined map/select method https://bugs.ruby-lang.org/issues/5663#change-25872 Author: wycats (Yehuda Katz) Status: Feedback Priority: Normal Assignee: matz (Yukihiro Matsumoto) 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/