Forum: Ruby What are lambda functions used for?

Posted by Mike Glaz (mikeglaz)
on 2013-02-14 21:06
I'm new to Ruby and lambda functions.  I understand how they work but
was wondering when would you use them?

thanks, mike
Posted by RKA (Guest)
on 2013-02-14 21:31
(Received via mailing list)
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?
Posted by Mike Glaz (mikeglaz)
on 2013-02-14 22:59
Ok, that makes sense.  Om, would you be able to post the Ruby code for 
the specific example you just stated?
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-02-14 23:28
(Received via mailing list)
On Thu, Feb 14, 2013 at 10:59 PM, Mike Glaz <lists@ruby-forum.com> 
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.
Posted by RKA (Guest)
on 2013-02-14 23:57
(Received via mailing list)
#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
Posted by Charles Caldwell (unfalseideas)
on 2013-02-15 16:51
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.
Posted by Robert Klemme (robert_k78)
on 2013-02-18 13:13
(Received via mailing list)
On Thu, Feb 14, 2013 at 9:31 PM, RKA <roshkins@gmail.com> 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:0x8003b698@(irb):4 (lambda)>>
irb(main):005:0> c.apply(2, 4)
=> 8
irb(main):006:0> c.apply(4, -3)
=> -12

Kind regards

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.