From: ko1@... Date: 2014-01-31T05:48:55+00:00 Subject: [ruby-core:60371] [ruby-trunk - Feature #8987] map/collect extension which handles arguments Issue #8987 has been updated by Koichi Sasada. (2013/10/06 0:26), sawa (Tsuyoshi Sawada) wrote: > [1, 2, 3, 4].map(&4.method(:+)) > # => [5, 6, 7, 8] Interesting. If we use �� (alias of lambda���, it is more short. module Kernel alias �� lambda end p [1, 2, 3, 4].map(&4.method(:+)) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��{|x| 4+x}) #=> [5, 6, 7, 8] If we define �� as the following definition, more short code. module Kernel def ��(a, sym) lambda{|x| a.send(sym, x)} end end p [1, 2, 3, 4].map(&��(4, :+)) #=> [5, 6, 7, 8] A bit shorter version. module Kernel def ��(expr) eval("lambda{|x| #{expr} x}") end end p [1, 2, 3, 4].map(&��("4+")) #=> [5, 6, 7, 8] If we have default parameter `_' (maybe matz doesn't like), we can make more short code. p [1, 2, 3, 4].map(&��{4+_}) #=> [5, 6, 7, 8] Summary: p [1, 2, 3, 4].map(&4.method(:+)) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��{|x| 4+x}) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��(4, :+)) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��("4+")) #=> [5, 6, 7, 8] p [1, 2, 3, 4].map(&��{4+_}) #=> [5, 6, 7, 8] (doesn't run) -- // SASADA Koichi at atdot dot net ---------------------------------------- Feature #8987: map/collect extension which handles arguments https://bugs.ruby-lang.org/issues/8987#change-44850 * Author: So Wieso * Status: Open * Priority: Normal * Assignee: * Category: core * Target version: current: 2.2.0 ---------------------------------------- Please consider extending map/collect by allowing additional arguments to be passed to proc, like: A: [1,2,3,4].map :+, 4 and/or B: [1,2,3,4].map 4, &:+ => [5, 6, 7, 8] Variant A is probably more readable. Variant B is more versatile (allows arbitrary arguments to be passed to block). ---Files-------------------------------- mappi.rb (410 Bytes) -- http://bugs.ruby-lang.org/