Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lambdas == closures == anonymous functions ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jorge Phillips
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic