[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

When are lambdas needed?

Stedwick

4/17/2008 9:34:00 PM

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?

Can somebody give me an extremely useful, NOT complicated, example of
when lambdas are the absolute perfect solution to a problem?

Thanks!
25 Answers

Avdi Grimm

4/17/2008 9:41:00 PM

0

On Thu, Apr 17, 2008 at 5:35 PM, Stedwick <philip.brocoum@gmail.com> wrote:
> Can somebody give me an extremely useful, NOT complicated, example of
> when lambdas are the absolute perfect solution to a problem?

Every time you use a block in ruby you're using a lambda implicitly.

--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Joel VanderWerf

4/17/2008 9:59:00 PM

0

Avdi Grimm wrote:
> On Thu, Apr 17, 2008 at 5:35 PM, Stedwick <philip.brocoum@gmail.com> wrote:
>> Can somebody give me an extremely useful, NOT complicated, example of
>> when lambdas are the absolute perfect solution to a problem?
>
> Every time you use a block in ruby you're using a lambda implicitly.

Implicitly, but not really. I wonder, was the original question more
like "what are blocks for?" or "yes, I know what blocks are for, but why
are there also these Proc objects, which are instantiated with the
lambda construct?"

If the latter, then one answer is: when you need the program state that
is encapsulated in a block to persist outside of the method in which it
is used. For example, this happens in event-based frameworks: a setup
method in a client class registers lambdas as event handlers; the
framework doesn't need to know the classes or methods of the client
class, hence loose coupling. As a bonus, these handlers can refer to
shared program state (local variables) in the setup method.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Minseok Choi

4/17/2008 11:39:00 PM

0

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?
>
> Sure, I can take a lambda and "pass it around" so to speak, but I can
> call a function from anywhere too, right?
>
> Can somebody give me an extremely useful, NOT complicated, example of
> when lambdas are the absolute perfect solution to a problem?
>
> 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 Choi

Daniel Waite

4/17/2008 11:58:00 PM

0

Joel VanderWerf wrote:
> If the latter, then one answer is: when you need the program state that
> is encapsulated in a block to persist outside of the method in which it
> is used. For example, this happens in event-based frameworks: a setup
> method in a client class registers lambdas as event handlers; the
> framework doesn't need to know the classes or methods of the client
> class, hence loose coupling. As a bonus, these handlers can refer to
> shared program state (local variables) in the setup method.

That's probably the clearest illustration I've ever read of when to use
a block. Thank you.

I hope others chime in, too, as I couldn't imagine programming without
the "implicit usage" of blocks (e.g. each, select, collect, etc.) but
have never used them for the purpose Joel described.

I just got "done" designing a delivery system, and although I'm happy
with it, I wonder if it could be done with blocks. (I re-read that
statement and it sounds stupid: 'done with blocks'. I'll simply trust
you see the absurdity of the statement and understand what I was trying
to say...)
--
Posted via http://www.ruby-....

ara.t.howard

4/18/2008 12:27:00 AM

0


On Apr 17, 2008, at 3:35 PM, Stedwick wrote:
> Can somebody give me an extremely useful, NOT complicated, example of
> when lambdas are the absolute perfect solution to a problem?


cache :index, :ttl => 42, :key => lambda{ request['important'] }


this is a class level method which says

"cache the index method, invalidating every 42 seconds, by using the
current request's 'important' value from the query"

note that in this cast the lambda will be instance_eval'd - so when we
say 'request' here it will ultimately mean the current request

another example:

def each &block

@list.each &block

end

here we need to have captured the calling block's scope in other to
relay it along to our internal @list object's each method. it's NOT
the case that we want the scope if the function for this lambda, what
we want is logic bound to the scope of the caller

lambda are perfect anytime you want a context sensitive result and
don't want to code everything in one massive global scope.



a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Marc Heiler

4/18/2008 12:48:00 AM

0

> lambda are perfect anytime you want a context sensitive result and
> don't want to code everything in one massive global scope.

My problem with lambda's is that I have a hard time to find a
real use case for them. I am not sure of some use case with
lambda {} that brings a definite advantage over i.e. just
using some special object or simple method.
--
Posted via http://www.ruby-....

Stefan Lang

4/18/2008 1:09:00 AM

0

2008/4/18, Marc Heiler <shevegen@linuxmail.org>:
> > lambda are perfect anytime you want a context sensitive result and
> > don't want to code everything in one massive global scope.
>
>
> My problem with lambda's is that I have a hard time to find a
> real use case for them. I am not sure of some use case with
> lambda {} that brings a definite advantage over i.e. just
> using some special object or simple method.

Technically, you can replace every lambda with
an class and an instance thereof. The difference is that
the lambda is syntactically and semantically more lightweight.

To add another example, Rake tasks store away lambdas.

my_lib_version = "1.0.2"

task :tgz do
sh "tar -czf my_lib-#{my_lib_version}.tgz lib"
end

Assuming a Ruby-like language without lambdas,
we'd get this "pure OO" Rakefile:

class TgzTask
def initialize(version)
@version = version
end
def execute
sh "tar -czf my_lib-#@version.tgz lib"
end
end

my_lib_version = "1.0.2"

task :tgz, TgzTask.new(my_lib_version)

All context we need in our method (local variables,
eventually the current "self") has to be explicetely
passed to the constructor, which has to initialize
our object etc. Rake would loose all its appeal.

Stefan

Arved Sandstrom

4/18/2008 1:19:00 AM

0

"Stedwick" <philip.brocoum@gmail.com> wrote in message
news:9568e4ae-6c60-4d8f-b460-7d9f414bcee9@l42g2000hsc.googlegroups.com...
>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?
>
> Can somebody give me an extremely useful, NOT complicated, example of
> when lambdas are the absolute perfect solution to a problem?
>
> Thanks!

One main use is precisely when you don't need to pass anything around - you
need a one-off function, and probably the function isn't particularly
complicated. Typical examples are when supplying a code block to a sort,
group, map or filter function.

Are they needed? No. But then again, neither are subroutines or modules or
for loops...

AHS


ara.t.howard

4/18/2008 1:40:00 AM

0


On Apr 17, 2008, at 6:47 PM, Marc Heiler wrote:
> My problem with lambda's is that I have a hard time to find a
> real use case for them. I am not sure of some use case with
> lambda {} that brings a definite advantage over i.e. just
> using some special object or simple method.

a lambda is a special object - on that knows about every variable in
scope. have fun maintaining code that populates that object by hand ;-)

the entire point of lambdas is that the language already *has* a
scope. so, sure, you can cherry pick the required variables and
populate an object, but you can also make loops with GOTO - lambda is
merely an abstraction.

the 'definite' bit comes in because you don't HAVE to do ANYTHING.
you simply write this code


sum = 0

list.each do |i|

sum += i

end

and you don't have to write anything special, the environment is
captured, the code is evaulated in the captured context, but you don't
have to build a special summing function that take i and a sum var.

so it 'definitely' is an advantage - that is unless you don't happen
to think less code is advantageous over more...

cheers.


a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




ara.t.howard

4/18/2008 1:44:00 AM

0


On Apr 17, 2008, at 6:47 PM, Marc Heiler wrote:
> My problem with lambda's is that I have a hard time to find a
> real use case for them



one more note

if you use

open(path){|fd| p fd.read}

array.each{|i| p i}

or

hash.each{|k,v| p k,v}

you've found a use for them ;-)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama