What are lambda functions used for?

I’m new to Ruby and lambda functions. I understand how they work but
was wondering when would you use them?

thanks, mike

Lambda functions are used when you want to call a function that doesn’t
necessarily need a name. For example, the each method of the array
object
iterates through all the items in the array and if you want to execute
the
same method over and over again with a different argument you can use a
lambda function, instead of having to define a function first.

Does that answer your question?

Ok, that makes sense. Om, would you be able to post the Ruby code for
the specific example you just stated?

On Thu, Feb 14, 2013 at 10:59 PM, Mike G. [email protected]
wrote:

Ok, that makes sense. Om, would you be able to post the Ruby code for
the specific example you just stated?

l = lambda {|x| p x}

l[1]
l[2]
l[3]

#or l.call(1)

Jesus.

#Okay.

#Say I have an array defined as follows:

my_array = %w{This is an array of words} #The %w is a fancy way of
making
an array separated where there’s spaces

It could also be done like this

my_array_also = [“This”, "is’, “an”, “array”, “of”, “words”] #Notice how
much quicker the above is.

#I can do this:
my_array.each{|array_element| print array_element + " "}
#Resulting in the output:
#This is an array of words

I find them useful when I’m writing scripts that automate tasks. I’ll
have many variables and I’ll have something I want to do to them over
and over with only a slight change. I could write a method but then I’ll
have to pass in all the variables which will make for a pretty nasty
looking parameter list. So, instead, I’ll just use a lambda.

roll_over_rows = lambda { |column|
7.upto(121) do |row|
# Do some stuff with the data in those rows
# Insert that data into a new place
# Do some calculations
end

Update variables with data

Put calculation into another place

excel_file.ws.range("#{column}127").value = some_variable +
another_variable
}

roll_over_rows[‘T’]
roll_over_rows[‘C’]

Called several more times

I’m probably breaking some serious design patterns but it is what I
personally find lambdas useful for.

On Thu, Feb 14, 2013 at 9:31 PM, RKA [email protected] wrote:

Lambda functions are used when you want to call a function that doesn’t
necessarily need a name.

I would put it differently: lambdas are used when you need to use a
function as an object which can be referenced and passed around.

For example, the each method of the array object
iterates through all the items in the array and if you want to execute the
same method over and over again with a different argument you can use a
lambda function, instead of having to define a function first.

That is probably not such a good example because #each already uses a
different mechanism by using the block passed to the call. Typically
one would do

array.each do |x|

logic 1

end

array.each do |x|

logic 2

end

lambdas are really most useful if you store them somewhere for later
execution. This example is not brilliant but I hope it demonstrates
the point:

irb(main):001:0> Calculator = Struct.new :function do
irb(main):002:1* def apply(x, y) function[x, y] end
irb(main):003:1> end
=> Calculator
irb(main):004:0> c = Calculator.new lambda {|a,b| a*b}
=> #<struct Calculator function=#<Proc:[email protected](irb):4 (lambda)>>
irb(main):005:0> c.apply(2, 4)
=> 8
irb(main):006:0> c.apply(4, -3)
=> -12

Kind regards

robert