• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between a Lambda and a Closure

 
Tim Cooke
Sheriff
Pie
Posts: 3144
136
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richard,

First up, an apology: I missed your talk at Devoxx yesterday as I was next door listening to John Smart talk about testing.

And now for the question: What is the difference between a Lambda and a Closure?

I thought I knew the answer to this but listening to Peter Ledbrook this afternoon talk about Groovy, he threw up some code on the screen, at which I though "oh yes, that's a Lambda expression", only for him to say "but this isn't a Lambda, it's a Closure". Now I'm a little confused over the distinction.

Thanks
Tim
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The talk yesterday was good.
Those of us who went to Devoxx have an opportunity to revisit it because there will be repeats of the talks available, so you can make up on the talk later.
 
Stephan van Hulst
Bartender
Posts: 6310
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A closure is a construct in which a function (possibly a lambda) is combined with variables that are not within local scope.

For instance:

This method returns a function that adds x to y. The function itself (in this case a lambda) references a variable that's not within its local scope (x). When you call the plus() method, that's when you create a closure that binds a value to x within the lambda.

y -> x+y is a lambda.
plus(3) creates a closure.
 
Stephan van Hulst
Bartender
Posts: 6310
77
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought of a further clarification. plus(3) creates a closure that you can imagine looking like this: y -> 3+y.

The difference between that, and actually writing down the lambda y -> 3+y is that the lambda resides in the source code, while the closure is created at runtime and resides in memory.
 
Sean Corfield
Ranch Hand
Posts: 305
11
Clojure Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An explanation I read the other day which I think helps is:
  • A lambda is a compile-time - syntax - construction.
  • A closure is a run-time - dynamic - construction.

  • A lot of people use the two terms interchangeably and others draw an incorrect distinction between them - which causes a lot of confusion.
     
    Tim Cooke
    Sheriff
    Pie
    Posts: 3144
    136
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Some good responses, thanks very much. This might take me a little more processing and re-reading of these answers before I 'get it' but I think it's starting to make some sense now.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic