[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Proc vs lambda vs proc

minkoo.seo@gmail.com

2/4/2007 3:55:00 AM

Hi group(and probably ruby-talk list - is it running btw?).

I got a question on Proc.new and lambda { ... }. AFAIK, there's two
differences between Proc.new(=proc) and lambda. The first one is that
Proc.new does not check the number of arguments passed to it while
lambda does. The secondis that lambda returns as we expect, i.e., it
returns value, while Proc.new does not.

Though Proc.new lacks several advantages that lambda has, I guess
there might be some situation where Proc.new is more suitable than
lambda. As an example, some code blocks that have to executed
thousands times might run more faster than lambda because it does not
have to check the # of arguments, thereby decreasing computational
overhead.

What is your opinion?

Sincerely,
Minkoo Seo

19 Answers

Robert Klemme

2/4/2007 11:18:00 AM

0

On 04.02.2007 04:55, Minkoo Seo wrote:
> I got a question on Proc.new and lambda { ... }. AFAIK, there's two
> differences between Proc.new(=proc) and lambda.

I think you have it slightly wrong: "proc" and "lambda" are aliases
while "Proc.new" works different.

> The first one is that
> Proc.new does not check the number of arguments passed to it while
> lambda does.

Correct:

irb(main):001:0> f1 = Proc.new {|a,b|a+b}
=> #<Proc:0x003bb2d0@(irb):1>
irb(main):002:0> f2 = proc {|a,b|a+b}
=> #<Proc:0x003b1690@(irb):2>
irb(main):003:0> f3 = lambda {|a,b|a+b}
=> #<Proc:0x003a4d3c@(irb):3>
irb(main):004:0> f1[1,2]
=> 3
irb(main):005:0> f1[1,2,3]
=> 3
irb(main):006:0> f2[1,2,3]
ArgumentError: wrong number of arguments (3 for 2)
from (irb):2
from (irb):6:in `[]'
from (irb):6
from :0
irb(main):007:0> f3[1,2,3]
ArgumentError: wrong number of arguments (3 for 2)
from (irb):3
from (irb):7:in `[]'
from (irb):7
from :0
irb(main):008:0> f2[1,2]
=> 3
irb(main):009:0> f3[1,2]
=> 3

> The secondis that lambda returns as we expect, i.e., it
> returns value, while Proc.new does not.

I do not understand what you mean here. All three return what you
expect (apart from an exception in the case of wrong # of arguments.

> Though Proc.new lacks several advantages that lambda has,

Which advantages do you refer to?

> I guess
> there might be some situation where Proc.new is more suitable than
> lambda. As an example, some code blocks that have to executed
> thousands times might run more faster than lambda because it does not
> have to check the # of arguments, thereby decreasing computational
> overhead.

I suggest to benchmark a concrete example if you are interested in
timings. My guess is that the parameter checking overhead is negligible.

> What is your opinion?

I usually use lambda because it most resembles the term "lambda
expression". But I guess this is just a matter of taste / personal
preference.

Kind regards

robert

Kalman Noel

2/4/2007 11:29:00 AM

0

Robert Klemme:
> On 04.02.2007 04:55, Minkoo Seo wrote:
>> I got a question on Proc.new and lambda { ... }. AFAIK, there's two
>> differences between Proc.new(=proc) and lambda.
> I think you have it slightly wrong: "proc" and "lambda" are aliases
> while "Proc.new" works different.

This is true for Ruby 1.8, but was changed in Ruby 1.9. The decision was
taken because proc and Proc.new are lexically too similar to have different
meanings, IIRC.

Kalman

Robert Klemme

2/4/2007 1:00:00 PM

0

On 04.02.2007 12:28, Kalman Noel wrote:
> Robert Klemme:
>> On 04.02.2007 04:55, Minkoo Seo wrote:
>>> I got a question on Proc.new and lambda { ... }. AFAIK, there's two
>>> differences between Proc.new(=proc) and lambda.
>> I think you have it slightly wrong: "proc" and "lambda" are aliases
>> while "Proc.new" works different.
>
> This is true for Ruby 1.8, but was changed in Ruby 1.9. The decision was
> taken because proc and Proc.new are lexically too similar to have different
> meanings, IIRC.

Thanks for the heads up! Me not being an early adopter tested with 1.8
only. At least I don't have to change my habit of using "lambda". :-)

Kind regards

robert

dblack

2/4/2007 1:02:00 PM

0

dblack

2/4/2007 1:26:00 PM

0

Jim Weirich

2/4/2007 7:38:00 PM

0

Minkoo Seo wrote:
> [...]
> The secondis that lambda returns as we expect, i.e., it
> returns value, while Proc.new does not.
>
> [...] I guess
> there might be some situation where Proc.new is more suitable than
> lambda. [...]

lambda and Proc.new do handle the return statement in different ways.
Return in lambda returns from the lambda, treating the lambda as an
anonymous function.

Return in Proc.new returns from the enclosing method, treating the block
as an inline piece of code.

The Proc.new behavior is useful where the block is simply a bit the
algorithm. For example:

def member?(target, list)
list.each do |item|
return true if target == item
end
false
end

The explicit return should return from the member? method, not just from
the loop.

-- Jim Weirich

--
Posted via http://www.ruby-....

minkoo.seo@gmail.com

2/5/2007 6:56:00 AM

0

On Feb 4, 8:18 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 04.02.2007 04:55, Minkoo Seo wrote:
> > there might be some situation where Proc.new is more suitable than
> > lambda. As an example, some code blocks that have to executed
> > thousands times might run more faster than lambda because it does not
> > have to check the # of arguments, thereby decreasing computational
> > overhead.
>
> I usually use lambda because it most resembles the term "lambda
> expression". But I guess this is just a matter of taste / personal
> preference.
>
> Kind regards
>
> robert

Okay. The problem is that I am writing a ruby tutorial for people from
other langauges (like C++ or Java), but there are three seemingly same
things: block, proc, and lambda. So I need to write about not only the
differences but also the simple but effective rule for choosing one
out of three.

To make it worse, the functionality provided by proc and lambda is
quite similar except for the differences in parameter checking and
return statement handling. And many people suggest to use lambda
instead of proc if one does not have any particular reason. So what
I'd like to do is to verify whether the preference for lambda is
correct or not.

So, my intention is to find out whether there are people who prefer
Proc to lambda whatever the reason is. Up to now, I see none. Any one?

Sincerely,
Minkoo Seo

minkoo.seo@gmail.com

2/5/2007 7:02:00 AM

0

On Feb 5, 4:37 am, Jim Weirich <j...@weirichhouse.org> wrote:
> Minkoo Seo wrote:
> > [...]
> > The secondis that lambda returns as we expect, i.e., it
> > returns value, while Proc.new does not.
>
> lambda and Proc.new do handle the return statement in different ways.
> Return in lambda returns from the lambda, treating the lambda as an
> anonymous function.
>
> Return in Proc.new returns from the enclosing method, treating the block
> as an inline piece of code.
>
> The Proc.new behavior is useful where the block is simply a bit the
> algorithm. For example:
>

I guess the following block (do |item| ... end)

> def member?(target, list)
> list.each do |item|
> return true if target == item
> end
> false
> end
> The explicit return should return from the member? method, not just from
> the loop.
>
> -- Jim Weirich

is block and not a proc.

AFAIK, block is not converted to proc unless the block is captured by
parameter like &blk. Am I wrong?

Sincerely,
Minkoo Seo

Patrick Fernie

2/5/2007 3:30:00 PM

0

I found the following:
http://innig.net/software/ruby/closures-...
to be an interesting read on the different flavors of closures in
ruby; I think it takes into account some of the 1.9 changes, but it
may be slightly outdated...

-P

On 2/5/07, Minkoo Seo <minkoo.seo@gmail.com> wrote:
> On Feb 5, 4:37 am, Jim Weirich <j...@weirichhouse.org> wrote:
> > Minkoo Seo wrote:
> > > [...]
> > > The secondis that lambda returns as we expect, i.e., it
> > > returns value, while Proc.new does not.
> >
> > lambda and Proc.new do handle the return statement in different ways.
> > Return in lambda returns from the lambda, treating the lambda as an
> > anonymous function.
> >
> > Return in Proc.new returns from the enclosing method, treating the block
> > as an inline piece of code.
> >
> > The Proc.new behavior is useful where the block is simply a bit the
> > algorithm. For example:
> >
>
> I guess the following block (do |item| ... end)
>
> > def member?(target, list)
> > list.each do |item|
> > return true if target == item
> > end
> > false
> > end
> > The explicit return should return from the member? method, not just from
> > the loop.
> >
> > -- Jim Weirich
>
> is block and not a proc.
>
> AFAIK, block is not converted to proc unless the block is captured by
> parameter like &blk. Am I wrong?
>
> Sincerely,
> Minkoo Seo
>
>
>

Gavin Kistner

2/5/2007 4:26:00 PM

0

On Feb 5, 8:29 am, "Patrick Fernie" <patrick.fer...@gmail.com> wrote:
> I found the following:http://innig.net/software/ruby/closures-...
> to be an interesting read on the different flavors of closures in
> ruby; I think it takes into account some of the 1.9 changes, but it
> may be slightly outdated...

Although certainly interesting, the author seems to have a slightly
too-broad definition of the term closure. He seems to think that
closures must be first-class functions in addition to providing
lexical binding to the environment they were defined in. That's simply
not so.

Further, he seems to be confusing the syntax of Ruby with features.
For example, in a function:
def foo( *a ); end
You refer to array of values by the variable 'a', not by '*a'; just
so, of course you refer to a block in a function
def foo( &b ); end
simply as 'b'...he seems hung up that you can't use &b directly.