Raw idea about Proc

Hi all.

There is the idea (in VERY dirty code):

···

--
class Proc
  def bound; @bound ||= {} end
  def bind(num, val)
    bound[num - 1] = val
  end

  alias :old_arity :arity
  def arity
    old_arity - bound.size
  end

  alias :old_call :call
  def call(*arg)
    bound.to_a.reverse_each{|num, val| arg.insert(num, val)}
    old_call(*arg)
  end
end
--

Usage:
--
l = lambda{|a,b,c| puts "a=#{a}, b=#{b}, c=#{c}"}

puts l.arity #=>3

l.bind(2, 'b')

puts l.arity #=>2

l.call('a', 'c') #a=a, b=b, c=c
--

Two questions:
1. Does somebody think it is useful?
2. Does somebody already done this in some library?

Thanks.

Victor.

Two questions:
1. Does somebody think it is useful?

Yes, this is called Currying and has been written about a lot.

2. Does somebody already done this in some library?

http://rubymurray.rubyforge.org/

Hope that helps.

James Edward Gray II

···

On May 8, 2006, at 9:07 AM, Victor Shepelev wrote:

There are a few drawbacks to this approach I think, most of which I
forget right now. A probable biggie is that it won't work with yield:

l = lambda { |num,ele| p num, ele }
l.bind(0,10)

[1,2].each &l
# 1
# nil
# 2
# nil

Maybe check out http://rubymurray.rubyforge.org , which does similar
stuff but by wrapping up the proc (still has limitations, but there you
go...)

···

On Mon, 2006-05-08 at 23:07 +0900, Victor Shepelev wrote:

Hi all.

There is the idea (in VERY dirty code):
--
class Proc
  def bound; @bound ||= {} end
  def bind(num, val)
    bound[num - 1] = val
  end

  alias :old_arity :arity
  def arity
    old_arity - bound.size
  end

  alias :old_call :call
  def call(*arg)
    bound.to_a.reverse_each{|num, val| arg.insert(num, val)}
    old_call(*arg)
  end
end

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk

> Two questions:
> 1. Does somebody think it is useful?

Yes, this is called Currying and has been written about a lot.

Oh, I know. The question meant "is this concrete extension would be useful
for this concrete Ruby?" :slight_smile:

> 2. Does somebody already done this in some library?

http://rubymurray.rubyforge.org/

Thanks, just what I've looked for.

Hope that helps.

Yeah, it did. :slight_smile:

James Edward Gray II

Victor.

···

From: James Edward Gray II [mailto:james@grayproductions.net]

On May 8, 2006, at 9:07 AM, Victor Shepelev wrote:

*cough* :slight_smile: I'd like to quote from the docs at the moment where I lost
the plot...

"You have a hole in your curried subroutine but want to add more spices
after or fill out holes after your first hole."

"Assuming no special spice before the hole, just put a hole in the hole."

···

On May 8, 2006, at 9:07 AM, Victor Shepelev wrote:

Two questions:
1. Does somebody think it is useful?

Yes, this is called Currying and has been written about a lot.

2. Does somebody already done this in some library?

http://rubymurray.rubyforge.org/

> Hi all.
>
> There is the idea (in VERY dirty code):
> --
> class Proc
> def bound; @bound ||= {} end
> def bind(num, val)
> bound[num - 1] = val
> end
>
> alias :old_arity :arity
> def arity
> old_arity - bound.size
> end
>
> alias :old_call :call
> def call(*arg)
> bound.to_a.reverse_each{|num, val| arg.insert(num, val)}
> old_call(*arg)
> end
> end

There are a few drawbacks to this approach I think, most of which I
forget right now. A probable biggie is that it won't work with yield:

Yes, I know (and disclaimer said about "VERY dirty code"). I've just tried
to show raw idea about providing arguments-binding functionality.

RubyMurray which you and James have pointed to, is quite good for this.

Ross Bamford - rosco@roscopeco.REMOVE.co.uk

Victor.

···

From: Ross Bamford [mailto:rossrt@roscopeco.co.uk]

On Mon, 2006-05-08 at 23:07 +0900, Victor Shepelev wrote:

Yeah, in truth it took me longer to figure out what the original Perl
docs were on about than it did to port the library :slight_smile: Happily though the
basic usage is pretty simple - just arguments and possibly regular
holes. You wouldn't need to be combining spice and using the other
special spices most of the time.

···

On Mon, 2006-05-08 at 23:43 +0900, benjohn@fysh.org wrote:

> On May 8, 2006, at 9:07 AM, Victor Shepelev wrote:
>
>> Two questions:
>> 1. Does somebody think it is useful?
>
> Yes, this is called Currying and has been written about a lot.
>
>> 2. Does somebody already done this in some library?
>
> http://rubymurray.rubyforge.org/

*cough* :slight_smile: I'd like to quote from the docs at the moment where I lost
the plot...

"You have a hole in your curried subroutine but want to add more spices
after or fill out holes after your first hole."

"Assuming no special spice before the hole, just put a hole in the hole."

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk