Arrray#to_proc

I don't know if this was posted previously, but I had the idea of a
Array#to_proc, an example would be:

[9, 19, 29].map(&[:succ, :to_s, :reverse])
=> ["01", "02", "03"]

I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')
but I think using the Array#to_proc solution is cleaner.

Would this actually prove useful? It would seem that using the normal
block form would be just as much typing, though.

···

--
Matt

This could very soon start to look like line noise:

e.g.
[9, 19, 29].map(&%w{succ to_s reverse})

Symbol#to_proc is one thing (although I don't like it), Array#to_proc is too out there, IMO.

···

On Jul 1, 2006, at 8:27 AM, Matthew Harris wrote:

I don't know if this was posted previously, but I had the idea of a
Array#to_proc, an example would be:

[9, 19, 29].map(&[:succ, :to_s, :reverse])
=> ["01", "02", "03"]

I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')
but I think using the Array#to_proc solution is cleaner.

Would this actually prove useful? It would seem that using the normal
block form would be just as much typing, though.

-- Matt

Matthew Harris wrote:

I don't know if this was posted previously, but I had the idea of a
Array#to_proc, an example would be:

[9, 19, 29].map(&[:succ, :to_s, :reverse])
=> ["01", "02", "03"]

I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')
but I think using the Array#to_proc solution is cleaner.

Would this actually prove useful? It would seem that using the normal
block form would be just as much typing, though.

Well, unless Array's got something else to do with #to_proc, it doesn't
seem to me a dab things per say. What might be more intersting though
is what I suggsed in the Symbol.to_proc is beautiful thead combined
with a way to force +self+ to be passed rather then the result. It
would be something like:

  [9, 19, 29].map.succ!.to_s!.reverse
  => ["01", "02", "03"]

Where the ! is forcing self to be returned rather then the regular
result. I'm not even sure this would conflict with the defintion of
#succ! either. Altghough it could conflict with other defintions of
bang methods that don't return self (rare but out there). Is ther an
alternate notation?

Oh. While were at it:

  class Hash
    def to_proc
      Proc.new { |o|
        each {|k,v| o.__send__("#{k}=", v)
      }
   end
end

Usage:

  class X
    attr_accessor :a, :b
    def initialize( &blk )
      blk[self]
    end
  end

  h = {:a=>1, :b=>2}
  X.new( &h )

Of course, it's still no as good as uniform parameters, eg no special
form for blocks. :frowning:

T.

Matthew Harris wrote:

I don't know if this was posted previously, but I had the idea of a
Array#to_proc, an example would be:

[9, 19, 29].map(&[:succ, :to_s, :reverse])
=> ["01", "02", "03"]

I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')
but I think using the Array#to_proc solution is cleaner.

Would this actually prove useful? It would seem that using the normal
block form would be just as much typing, though.

I'd rather do this:

class Proc
   def +(other)
     proc {|*args|
       args = [self.call(*args)]
       other.call(*args)
     }
   end
end

[9, 19, 29].map( &:succ + &:to_s + &:reverse )
#=> ["01", "02", "03"]

but I'm just weird like that :slight_smile:

Is this useful for anything? It looks like it should be, but I can't think of what...

···

--
Alex

Hi,

I get a "parse error, unexpected tAMPER" with ruby 1.8.4 (2005-12-24)
[i386-mswin32].
What version are you using?

How about something like this:

module Enumerable
  def cascade(*methods)
    methods.inject(self) { |ary, method| ary.map{ |x| x.send(method)}}
  end
end

res = [9, 19, 29].cascade :succ, :to_s, :reverse
p res
#=> ["01", "02", "03"]

Unless you prefer the line noise of course :wink:

Regards,
Sean

···

On 7/1/06, Alex Young <alex@blackkettle.org> wrote:

Matthew Harris wrote:
> I don't know if this was posted previously, but I had the idea of a
> Array#to_proc, an example would be:
>
> [9, 19, 29].map(&[:succ, :to_s, :reverse])
> => ["01", "02", "03"]
>
> I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')
> but I think using the Array#to_proc solution is cleaner.
>
> Would this actually prove useful? It would seem that using the normal
> block form would be just as much typing, though.
>
I'd rather do this:

class Proc
   def +(other)
     proc {|*args|
       args = [self.call(*args)]
       other.call(*args)
     }
   end
end

[9, 19, 29].map( &:succ + &:to_s + &:reverse )
#=> ["01", "02", "03"]

but I'm just weird like that :slight_smile:

Is this useful for anything? It looks like it should be, but I can't
think of what...

--
Alex

Alex Young <alex@blackkettle.org> writes:

Matthew Harris wrote:

I don't know if this was posted previously, but I had the idea of a
Array#to_proc, an example would be:
[9, 19, 29].map(&[:succ, :to_s, :reverse])
=> ["01", "02", "03"]
I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')
but I think using the Array#to_proc solution is cleaner.
Would this actually prove useful? It would seem that using the
normal
block form would be just as much typing, though.

I'd rather do this:

class Proc
  def +(other)
    proc {|*args|
      args = [self.call(*args)]
      other.call(*args)
    }
  end
end

[9, 19, 29].map( &:succ + &:to_s + &:reverse )
#=> ["01", "02", "03"]

but I'm just weird like that :slight_smile:

Lovely. Too bad we can't overload "." :wink:

···

Alex

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Alex Young wrote:

[9, 19, 29].map(&[:succ, :to_s, :reverse])
=> ["01", "02", "03"]

I'd rather do this:

...

[9, 19, 29].map( &:succ + &:to_s + &:reverse )
#=> ["01", "02", "03"]

I also wrote similar code for my project:

  http://rubyforge.org/cgi-bin/viewvc.cgi/blockutils/?root=blockutils

That includes the other utility functions for ruby-1.9, but can't work with ruby-1.8.

Regards,

···

--
Takaaki Tateishi <ttate@ttsky.net>

Sean O'Halpin wrote:

···

On 7/1/06, Alex Young <alex@blackkettle.org> wrote:

Matthew Harris wrote:
> I don't know if this was posted previously, but I had the idea of a
> Array#to_proc, an example would be:
>
> [9, 19, 29].map(&[:succ, :to_s, :reverse])
> => ["01", "02", "03"]
>
> I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')
> but I think using the Array#to_proc solution is cleaner.
>
> Would this actually prove useful? It would seem that using the normal
> block form would be just as much typing, though.
>
I'd rather do this:

class Proc
   def +(other)
     proc {|*args|
       args = [self.call(*args)]
       other.call(*args)
     }
   end
end

[9, 19, 29].map( &:succ + &:to_s + &:reverse )
#=> ["01", "02", "03"]

but I'm just weird like that :slight_smile:

Is this useful for anything? It looks like it should be, but I can't
think of what...

--
Alex

Hi,

I get a "parse error, unexpected tAMPER" with ruby 1.8.4 (2005-12-24)
[i386-mswin32].
What version are you using?

Sorry, it assumes a working Symbol#to_proc that's supported with that syntax... The proc chaining is what's interesting from my point of view.

--
Alex