Closure and pure function?

Doesn’t idea of closures go against pure function?With closures, we make function dependent on an external variable,something we don’t want to have for a pure function.

Closing over a val is pure, closing over a var isn’t.

2 Likes

I and some others tend to use @runarorama’s definition, which is adapted in Functional Programming in Scala, as a useful one (the justification for “useful” is included in that post):

A function f is pure if the expression f(x) is referentially transparent for all referentially transparent x.

And “pure function with no free variables” gets the name combinator.

Can I haz @combinator annotation then?

Seriously, it would be really nice to have a way to stop accidently closing
over state. This is also a performance optimization, as such a function can
be a singleton.

There was a project to provide that, called Spores. I’m not sure how far it
got.

Spores appears abandoned, and was much more ambitious than simply ensuring,
effectively, that the generated lambda constructor is empty.