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