aspose file tools*
The moose likes Other Languages and the fly likes Lambdas == closures == anonymous functions ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Lambdas == closures == anonymous functions ?" Watch "Lambdas == closures == anonymous functions ?" New topic
Author

Lambdas == closures == anonymous functions ?

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Hi,

I've been reading up on some functional programming literature and
am a bit confused by the proliferation of terminology for "first class functions".

1) I understand that the big difference between Java's anonymous function with closures is that the former cannot access free variables. Anonymous functions can be passed into methods as well as returned as return values.

2) python has a construct called lambdas. I've never programmed in python; thus I would like to know if they just another name for closures.

Regards,

Pho


Regards,

Pho
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
I normally view a "lambda", "lambda function", or "lambda operator" as what's used to create a closure. Ie the result of evaluating a lambda expression is a closure.
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375
I'm no expert (just trying to learn the functional paradigm too), as far as I know, lambda is the name for anonymous functions (should be equivalent to lambda functions too). Java doesn't have anonymous functions, they're simulated through anonymous classes. (there's a huge debate on adding them, because they're needed for closures)
A closure is a little different, for what I could understand from the wikipedia article, is the combination of the anonymous function with the other variables referenced by it.


Gabriel
Software Surgeon
Jorge Phillips
Ranch Hand

Joined: Jun 03, 2001
Posts: 43
Gabriel is correct. A lambda expression by itself is not a closure. A closure is a construct that binds a lambda expression to an evaluation context (lexical or dynamic)used to lookup any free variables (functional or not) in the lambda body. Passing around closures thus allows evaluation of a lambda expression to be done in the environment where the closure was created as opposed to the environment where the closure is evaluated.

As an example: if you have two separate evaluation stacks A and B and you create a closure in one of the stacks (say A) and pass it as an object to a function that is executing in the other stack (say B), then evaluation in stack B of the lambda expression in the closure that was created in stack A will be done in the environment context in stack A where the closure was created. Therefore the name closure. It closes the evaluation environment of the associated lambda to be the environment where the closure is created, regardless of where the closure is evaluated.

As you see, this mechanism is very powerful for creating complex control structures and evaluation regimes.
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 460
Originally posted by Jorge Phillips:
[QB]if you have two separate evaluation stacks A and B and you create a closure in one of the stacks (say A) and pass it as an object to a function that is executing in the other stack (say B), then evaluation in stack B of the lambda expression in the closure that was created in stack A will be done in the environment context in stack A where the closure was created.[QB]


I was lost for some time there, but finally got it. Thanks


Visit my blog: http://jnassef.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lambdas == closures == anonymous functions ?