0

By the definition closures are

Scala Closures are functions which use one or more free variables and the return value of this function is dependent of these variable. The free variables are defined outside of the Closure Function and is not included as a parameter of this function.

and the definition of a pure function is

A pure function is a function that depends only on its declared inputs and its internal algorithm to produce its output. It does not read any other values from “the outside world” — the world outside of the function's scope — and it does not modify any values in the outside world.

when functional programming is all about writing code in terms of pure functions,: how come concept like closure is justified in functional programming

please help to clear the understanding

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Vivek
  • 405
  • 1
  • 4
  • 14
  • 2
    Scala does not claim to be a pure functional language, though. – Sergio Tulentsev Oct 18 '20 at 09:44
  • 1
    but a functional programming language like erlang also has closures.how? – Vivek Oct 18 '20 at 09:53
  • 1
    Overall, I think your hypothesis ("functional programming is all about writing code in terms of pure functions") is flawed to begin with, but I don't have enough theoretical knowledge to properly refute it. – Sergio Tulentsev Oct 18 '20 at 09:55
  • 3
    You seem to be confused by "*It does not read any other values from “the outside world”*". This doesn't mean that a pure function is not allowed to access variables defined outside its body, it means that it must not execute a "read" *action* on *stateful* free variables. Accessing closed-over constants from a higher scope is totally fine and doesn't affect purity. – Bergi Oct 18 '20 at 10:58
  • 1
    The free variables may be "outside of the Closure Function" but they are not "outside of the function's scope". They must be in scope in order to be visible to the function. – Tim Oct 18 '20 at 12:22

1 Answers1

1

Please consider the following example

def factorial(n: Int): Int = {
  lazy val loop: (Int, Int) => Int = 
    (i, acc) =>
      if (i == n + 1) acc
      else loop(i + 1, acc * i)

  loop(1, 1)
}

This is tail-recursive version of factorial with iterating from 1 to n.

From FP point of view all functions here (either Scala methods or actual scala.Functions) are pure i.e. on the same inputs they return the same output and don't have side effects, their calls can be replaced with their results (referential transparency).

But loop being a closure depends on a parameter from outer scope (namely n).

Reading a variable (free of side-effects) from outer scope (on contrary to writing) is not considered a side effect.

https://alvinalexander.com/scala/fp-book/definition-of-pure-function/

https://en.wikipedia.org/wiki/Pure_function

https://en.wikipedia.org/wiki/Referential_transparency

What is referential transparency?

https://en.wikipedia.org/wiki/Closure_(computer_programming)

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • What is the reason for using a `lazy val` rather than `def`? Are there performance differences or is it just a question of style? – Tim Oct 18 '20 at 14:54
  • @Tim Well, generally choice `def`/`lazy val` is surely not a question of style because they have different semantics. Here this was just for illustrating purpose, to make lambda/closure more evident. Normally I would use a method and nested method. Regarding performance methods can have a little better performance than functions. – Dmytro Mitin Oct 18 '20 at 15:57