When are lambdas needed?

D

David A. Black

Hi --

Another difference between lambdas and procs, is that procs are less
strict about arity when they are called.

Yes, assuming that by "proc" you mean Proc.new and not proc....
1: p = Proc .new {|x,y| [x,y]}
2: l = lambda {|x,y| [x, y]}
3:
4: p.call(1) # => [1, nil]
5: l.call # =>
# ~> -:5: wrong number of arguments (0 for 2) (ArgumentError)
# ~> from -:5:in `call'
# ~> from -:5

procs are invoked using the same semantics as a yield, which acts like
parallel assignment with nils being provided for missing arguments.

lambdas are invoked using the same semantics as a message send.
Since it's a bit confusing to have proc and Proc.new be different,
this has been changed in 1.9: proc and Proc.new are the same in 1.9,
and lambda is different.

(I still have no idea how to memorize which is which.)

Thinking about this a bit, I think that the basic rule is that procs
act like blocks, in that:

arguments are passed to them using yield semantics
and
return effects a return from the activation of the code containing
the source of the block
break breaks out of the block into the activation of the code
containing the source of the block

Lambdas act like methods in that:

arguments are passed to them using method call semantics
and
return effects a return from the body of the lambda
break breaks out of the body of the lambda, back to the caller,
effectively becoming a return

So a double mnemonic might be:

proc rhymes with block
lambda and method both have and m while proc does not.

The problem is that proc also rhymes with Proc :)


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
 
K

Ken Bloom

Hi --



It's actually a difference between lambda and proc (which are synonyms),
on the one hand, and Proc.new on the other:

irb(main):009:0> lambda { return }.call => nil
irb(main):010:0> proc { return }.call => nil
irb(main):011:0> Proc.new { return }.call LocalJumpError: unexpected
return

Since it's a bit confusing to have proc and Proc.new be different, this
has been changed in 1.9: proc and Proc.new are the same in 1.9, and
lambda is different.

(I still have no idea how to memorize which is which.)

That's why I tried to be intentionally vague about the differences, but I
guess I wasn't vague enough. Thanks everybody for filling in the details.

--Ken
 
R

Robert Klemme

I have seen many tutorials on the Internet explaining where lambdas
CAN be used, such as clever multiplication functions, but when are
they actually NEEDED?

Well, there is always a workaround, so no language feature is actually
"needed" in the strict sense of the word.
Sure, I can take a lambda and "pass it around" so to speak, but I can
call a function from anywhere too, right?

Yes, but a lambda is an object which makes certain things very nice.
Can somebody give me an extremely useful, NOT complicated, example of
when lambdas are the absolute perfect solution to a problem?

I like to use them when I have to efficiently choose an algorithm based
on some value:

algos = {
"print" => lambda {|x| puts x},
"ignore" => lambda {|x| },
"log" => lambda {|x| File.open("log","w") {|io| io.puts x}},
}

Now you can efficiently call a function based on some input

....each do |x,y|
algos[x][y]
end

Yes, I know the example is artificial and yes, you can do it with an
object and #send as well. But if keys are not Strings or have different
types then this approach is simpler.

Kind regards

robert
 
R

Rick DeNatale

The problem is that proc also rhymes with Proc :)

Yes this is one good thing that 1.9 is doing.

In 1.8 I pronounce Proc, as in Prock, and proc as in lambda.

Actually I NEVER use proc in 1.8, rarely use Proc.new, fairly
frequently use lambda.
 
B

Brian Adkins

Lambdas are anonymous function.

As long as you can give a name to all the functions, they're not
needed, you can just define (ie. name) a function, and give that
function by name instead of using lambda.

But that's the very point! One gets fed up in having to come with
names for ALL the little functions we can encounter in a program.

So what is an eminent lisper doing posting on c.l.r - considering a
switch? :)
 
C

Christopher Dicely

I have seen many tutorials on the Internet explaining where lambdas
CAN be used, such as clever multiplication functions, but when are
they actually NEEDED?

Sure, I can take a lambda and "pass it around" so to speak, but I can
call a function from anywhere too, right?

Sure, you can call methods from almost anywhere. But you can't pass them
around (unless you turn them into proc objects, in which case you are back to
using lambdas.) And passing them around is necessary if you didn't write (and
don't want to have to monkeypatch) all the library, etc., code from which you
may want to perform a particular task. If those libraries are written to let you
pass in proc objects, you are in good shape.
Can somebody give me an extremely useful, NOT complicated, example of
when lambdas are the absolute perfect solution to a problem?

Callbacks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top