From: "prijutme4ty (Ilya Vorontsov)" Date: 2012-05-24T18:21:58+09:00 Subject: [ruby-core:45208] [ruby-trunk - Feature #6483] parametric map Issue #6483 has been updated by prijutme4ty (Ilya Vorontsov). nobu (Nobuyoshi Nakada) wrote: > =begin > I proposed a different approach, symbol with arguments syntax. > > (1..5).map(&:to_s(2)) #=> ['1', '10', '11', '100', '101'] > > And another proposed (({Symbol#call})) in the same thread, instead. > > (1..5).map(&:to_s.(2)) #=> ['1', '10', '11', '100', '101'] > =end Really cool syntax! I didn't even thought that about such a way. Second version I wrote like that: class Symbol def call(*args) obj=Object.new.instance_exec(self,args){|sym,params| @sym=sym; @args = params; self} obj.define_singleton_method :to_proc do @sym.to_proc.curry_except_self(*@args) end obj end end Unfortunately symbol isn't cloneable, so I used auxiliary object In such a way one shouldn't define both tap and map and pleorth of other methods! But first approach you suggested cannot be implemented in ruby yet. Hope sometimes ruby'll supply such a syntax! I propose it shouldn't even have parentheses for args ---------------------------------------- Feature #6483: parametric map https://bugs.ruby-lang.org/issues/6483#change-26793 Author: prijutme4ty (Ilya Vorontsov) Status: Open Priority: Normal Assignee: Category: Target version: I found very common use-case for map: map with parameters and &:meth syntax. For example: matrix =[[1,2,3],[4,5,6]] matrix.pmap(' ',&:join).join("\n") # => "1 2 3\n4 5 6 [1,2,3,4,5].pmap(2, &:to_s) # ['1', '10', '11', '100', '101'] [1,2,3,4,5].pmap(&:to_s) # ['1', '2, '3', '4', '5'] # empty parameter list behaves as usual map Isn't it much better than ugly and verbose code: matrix.map{|line| line.join(' ')}.join("\n") I can write simple implementation class Proc def curry_except_self(*args) Proc.new{|slf| curry[slf,*args] } end end module Enumerable def pmap!(*args,&block) map! &block.curry_except_self(*args) end def pmap(*args,&block) dup.pmap!(*args, &block) end end Use-cases can be rewritten as tests (I can send my own unit-test if needed) Also I've two related things to discuss. 1) First is &-syntax. Is it possible to change ruby-interpeter in such a way that &:proc could be at any place. matrix.pmap(&:join,' ') is much prettier than matrix.join(' ',&:join) What is the reason behind this restriction? And if one can remove this restriction, we'd have new nice syntax. 2) I'm not very experience in functional programming with curry etc, but it seems to me that currying proc without supplying first argument(self) can be common task when &:meth syntax is in play. If so, may be my curry_except_self(*args) also should be included in ruby. -- http://bugs.ruby-lang.org/