Schildmeijer
Schildmeijer

Reputation: 20946

Scala closures on wikipedia

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?

Upvotes: 9

Views: 4911

Answers (3)

Abubacker Siddik
Abubacker Siddik

Reputation: 77

As far as I understand, this is a closure that contains a formal parameter, threshold and context variable, bookList, from the enclosing scope. So the return value(List[Any]) of the function may change while applying the filter predicate function. It is varying based on the elements of List(bookList) variable from the context.

Upvotes: 0

Flaviu Cipcigan
Flaviu Cipcigan

Reputation: 7243

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), threshold is a free variable, as the function literal itself doesn't give it any meaning. By itself, _.sales >= threshold is 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:

def makeIncrementer(inc: Int): (Int => Int) = (x: Int) => x + inc

At runtime, the following code produces 3 closures. It's also interesting to note that b and c are not the same closure (b == c gives false).

val a = makeIncrementer(10)
val b = makeIncrementer(20)
val c = makeIncrementer(20)

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 with val f = new Foo, saying that f is the object.

-- Flaviu Cipcigan

Notes:

  • Reference: Programming in Scala, Martin Odersky, Lex Spoon, Bill Venners
  • Code compiled with Scala version 2.7.5.final running on Java 1.6.0_14.

Upvotes: 14

Jeremy Powell
Jeremy Powell

Reputation: 3486

I'm not entirely sure, but I think you're right. Doesn't a closure require state (I guess free variables...)?

Or maybe the bookList is the free variable?

Upvotes: 1

Related Questions