Found the following snippet on the Closure page on wikipedia
//# Return a list of all books with at least 'threshold' copies sold.
def bestSellingBooks(threshold: Int) = bookList.filter(book => book.sales >= threshold)
//# or
def bestSellingBooks(threshold: Int) = bookList.filter(_.sales >= threshold)
Correct me if I’m wrong, but this isn’t a closure? It is a function literal, an anynomous function, a lambda function, but not a closure?
Well… if you want to be technical, this is a function literal which is translated at runtime into a closure, closing the open terms (binding them to a val/var in the scope of the function literal). Also, in the context of this function literal (
_.sales >= threshold),thresholdis a free variable, as the function literal itself doesn’t give it any meaning. By itself,_.sales >= thresholdis an open term At runtime, it is bound to the local variable of the function, each time the function is called.Take this function for example, generating closures:
At runtime, the following code produces 3 closures. It’s also interesting to note that b and c are not the same closure (
b == cgivesfalse).I still think the example given on wikipedia is a good one, albeit not quite covering the whole story. It’s quite hard giving an example of actual closures by the strictest definition without actually a memory dump of a program running. It’s the same with the class-object relation. You usually give an example of an object by defining a
class Foo { ...and then instantiating it withval f = new Foo, saying that f is the object.-- Flaviu CipciganNotes: