When are lambdas needed?

Hi –

On Fri, 18 Apr 2008, Rick DeNatale wrote:

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…

parallel assignment with nils being provided for missing arguments.
act like blocks, 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 :slight_smile:

David

Stedwick wrote:

Thanks!
lambda is like a function pointer in c/c++.
Let me give you an example.

hello = lambda { “Hello” }
hello.call
=> “Hello”
hello = lambda { “World” }
hello.call
=> “World”

HTH,
Minseok C.

On 17.04.2008 23:33, Stedwick wrote:

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

On Apr 18, 5:13 am, [email protected] (Pascal J. Bourguignon)
wrote:

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? :slight_smile:

On Thu, Apr 17, 2008 at 2:35 PM, Stedwick [email protected]
wrote:

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.

On Fri, Apr 18, 2008 at 8:48 AM, David A. Black [email protected]
wrote:

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 :slight_smile:

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.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/