Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Proc vs lambda vs proc

Reply
Thread Tools

Proc vs lambda vs proc

 
 
Minkoo Seo
Guest
Posts: n/a
 
      02-04-2007
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-04-2007
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
 
Reply With Quote
 
 
 
 
Kalman Noel
Guest
Posts: n/a
 
      02-04-2007
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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-04-2007
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
 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      02-04-2007
Hi --

On Sun, 4 Feb 2007, 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.


Yes -- this was something that was raised at RubyConf in 2002 or 2003,
and Matz agreed that having proc != Proc.new, while proc == lambda,
was confusing.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      02-04-2007
Hi --

On Sun, 4 Feb 2007, Robert Klemme wrote:

> 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".


Originally, I think the decision was to deprecate 'proc' entirely --
which would be fine with me, though as long as it's not different from
Proc.new that's OK too.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Jim Weirich
Guest
Posts: n/a
 
      02-04-2007
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-forum.com/.

 
Reply With Quote
 
Minkoo Seo
Guest
Posts: n/a
 
      02-05-2007
On Feb 4, 8:18 pm, Robert Klemme <(E-Mail Removed)> 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

 
Reply With Quote
 
Minkoo Seo
Guest
Posts: n/a
 
      02-05-2007
On Feb 5, 4:37 am, Jim Weirich <(E-Mail Removed)> 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

 
Reply With Quote
 
Patrick Fernie
Guest
Posts: n/a
 
      02-05-2007
I found the following:
http://innig.net/software/ruby/closures-in-ruby.rb
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 <(E-Mail Removed)> wrote:
> On Feb 5, 4:37 am, Jim Weirich <(E-Mail Removed)> 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
>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type of lambda function returning a lambda function... Haochen Xie C++ 4 03-17-2013 11:23 PM
lambda vs non-lambda proc Steve Dogers Ruby 1 03-30-2009 10:11 PM
using lambda/Proc can prevent a lot of garbage collection Eric Mahurin Ruby 25 10-11-2005 03:01 AM
Convert VB.NET to TSQL PROC & Reference a Proc from another Proc David Lozzi ASP .Net 3 06-01-2005 06:35 PM
Re: Lambda as declarative idiom (was RE: what is lambda used for inreal code?) Roman Suzi Python 13 01-07-2005 09:33 PM



Advertisments