From: pjb@... (Pascal J. Bourguignon) Date: 2008-04-18T18:15:10+09:00 Subject: Re: When are lambdas needed? Stedwick writes: > 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? 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. Like if you went on a road, and gave a name to all the little peebles you encounter, keeping with you a picture of each peeble with the associated (unique!) name in your notebook. What a big useless notebook! Also, lambdas (closures actually) are equivalent to classes (objects). So, as long as you can write a class (give it a name!), and instanciate an object, everywhere you would use lambdas, they're not needed. But what a burden! Compare: ------------------------------------------------------------------------ def example array=[1,2,3,4] array.each{|i| print i," -> ",i+i,"\n" } array.each{|i| print i," -> ",i*i,"\n" } ... :done end ------------------------------------------------------------------------ vs. ------------------------------------------------------------------------ def printDouble(i) print i," -> ",i+i,"\n" end def printSquare(i) print i," -> ",i*i,"\n" end ... class Array def callForEach(f) for i in 0..self.size()-1 do f.call(self[i]) end end end def example array=[1,2,3,4] array.callForEach(method(:printDouble)) array.callForEach(method(:printSquare)) :done end ------------------------------------------------------------------------ and compare: ------------------------------------------------------------------------ def example k=42 array=[1,2,3,4] array.each{|i| print i," -> ",i+k,"\n" } array.each{|i| print i," -> ",i*k,"\n" } :done end ------------------------------------------------------------------------ vs. ------------------------------------------------------------------------ class Adder def initialize(k) @k=k end def run(i) print i," -> ",i+@k,"\n" end end class Multiplier def initialize(k) @k=k end def run(i) print i," -> ",i*@k,"\n" end end class Array def sendRunWithEach(o) for i in 0..self.size()-1 do o.run(self[i]) end end end def example k=42 array=[1,2,3,4] array.sendRunWithEach(Adder.new(k)) array.sendRunWithEach(Multiplier.new(k)) :done end ------------------------------------------------------------------------ So if you have a lot of time, a lot of memory, and a lot of fingers, perhaps you can do without lambda, but us poor mortals just need it. -- __Pascal Bourguignon__