Unique Marketing, Guaranteed Results.

Lambdas

July 27th, 2010 by Narshlob

What are these Lambdas you speak of?

This article is focused on Lambdas as used in the Ruby language.
What are Lambdas? They’re given several names in other languages.

  • Lambda
  • Anonymous Function
  • Closure

In Ruby, we just call it a Lambda function. It’s defined as such:

x = lambda { return "ar har har har" }

Calling this method will return “ar har har har” as a return value. If that were put into a function, like so,

def foo
  x = lambda { return "ar har har har" }
  x.call
  return "yo ho ho ho"
end

This will actually return “yo ho ho ho”. If you were to puts x.call, however, you would see “ar har har har”. Very interesting. So returning from lambda acts just as a function would, hence it’s an anonymous function.

Lambdas have an interesting quirk in that if you declare one as such:

x = lambda { |x, y| puts x + y }

And then call it like this:

x.call(1, 2, 3)

It will throw an argument error writing an essay for graduate school

Filed under: Uncategorized — Narshlob @ 3:52 pm on July 27, 2010

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Copyright © 2005-2016 PMA Media Group. All Rights Reserved &nbsp