• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Lambdas == closures == anonymous functions ?

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 194
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 471
Mac OS X Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Goodbye moon men. Hello tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic