Feature #6483

parametric map

Added by Ilya Vorontsov almost 2 years ago. Updated over 1 year ago.

[ruby-core:45198]
Status:Rejected
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:next minor

Description

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 curryexceptself(args)
Proc.new{|slf| curry[slf,
args] }
end
end

module Enumerable
def pmap!(args,&block)
map! &block.curryexceptself(
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 curryexceptself(*args) also should be included in ruby.


Related issues

Related to ruby-trunk - Feature #4146: Improvement of Symbol and Proc Rejected 12/10/2010

History

#1 Updated by Ilya Vorontsov almost 2 years ago

Also I propose similar approach for tap method.

class Object
def ptap(args,&block)
tap &block.curryexceptself(
args)
end
end

So instead of
lines = File.read('filename.txt');
lines.delete('')
or
lines = File.read('filename.txt').tap{|x|x.delete ''}

I can use
lines = File.read('filename.txt').ptap('', &:delete)

#2 Updated by Nobuyoshi Nakada almost 2 years ago

=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

#3 Updated by Ilya Vorontsov almost 2 years ago

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.instanceexec(self,args){|sym,params| @sym=sym; @args = params; self}
obj.define
singletonmethod :toproc do
@sym.toproc.curryexcept_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

#4 Updated by Benoit Daloze almost 2 years ago

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

For info, this is http://bugs.ruby-lang.org/issues/show/4146 (I had a hard time finding it back).

Another syntax is proposed by Koichi (from what I can understand):

p %w[12 45 32].map(&PM.to_i(9)).map(&PM * 2)
p %w[abc def ghi].map(&PM[1])

Which might be simplified to:

p %w[12 45 32].map(&.to_i(9)).map(& * 2)
p %w[abc def ghi].map(&[1])

I really like that one.

#5 Updated by Thomas Sawyer almost 2 years ago

=begin

p %w[12 45 32].map(&.to_i(9)).map(& * 2)

That's pretty neat. I wonder about its implementation, so basically & becomes a special object that returns a proc when method is called on it?

class Ampersand < BasicObject
def methodmissing(s, *a, &b)
::Proc.new{ |x| x.public
send(s, *a, &b) }
end
end

& = Ampersand.new

This is also interesting in that it has an appearance similar to an anaphora (default block argument):

p %w[12 45 32].map{it.to_i(9)}.map{it * 2}

But despite appearances they are very different in nature.
=end

#7 Updated by Ilya Vorontsov almost 2 years ago

Eregon (Benoit Daloze) wrote:

Another syntax is proposed by Koichi (from what I can understand):

p %w[12 45 32].map(&PM.to_i(9)).map(&PM * 2)
p %w[abc def ghi].map(&PM[1])

Which might be simplified to:

p %w[12 45 32].map(&.to_i(9)).map(& * 2)
p %w[abc def ghi].map(&[1])

I really like that one.

It's fine, but in version with PM it look like a hack. However if it'll be implemented in second version - I'd take my words back.

#8 Updated by Yusuke Endoh almost 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

Assigning to matz.

Eregon (Benoit Daloze) wrote:

For info, this is http://bugs.ruby-lang.org/issues/show/4146 (I had a hard time finding it back).

Thanks Benoit! I added it as a related ticket.

Yusuke Endoh mame@tsg.ne.jp

#9 Updated by Ilya Vorontsov almost 2 years ago

I've made a simple extension that allows one to use nested symbolic-procs like this:
[[1,2,3],[4,5,6]].map(&:map.(&:to_s.(2))) # => [['1','10','11'],['100','101','110']]

Not to duplicate code - look at http://bugs.ruby-lang.org/issues/4146#change-26991

#10 Updated by Nobuyoshi Nakada almost 2 years ago

=begin
In ((%golf_prelude.rb%)):

class Symbol
def call(*args, &block)
proc do |recv|
recv.send(self, *args, &block)
end
end
end

It might use (({public_send})), indeed.
=end

#11 Updated by Ilya Vorontsov almost 2 years ago

Thank you for much more elegant code. It works with all of my specs except one:
['abc','cdef','xy','z','wwww'].select(&:size.() == 4) # ==> ['cdef', 'wwww']

It looks that one cannot make this work without proxy object.

nobu (Nobuyoshi Nakada) wrote:

=begin
In ((%golf_prelude.rb%)):

class Symbol
def call(*args, &block)
proc do |recv|
recv.send(self, *args, &block)
end
end
end

It might use (({public_send})), indeed.
=end

#12 Updated by Yusuke Endoh over 1 year ago

  • Target version set to next minor

#13 Updated by Yukihiro Matsumoto over 1 year ago

  • Status changed from Assigned to Rejected

I reject the original idea of #pmap which might be read as 'parallel map' or 'parametric map' or something else.
The idea of adding parameter to block with argument specified e.g. a.map(&:to_s.(2)) is interesting,
but it must be proposed in separated issue.

Matz.

Also available in: Atom PDF