Scala closures
A closure is a function. The resultant function value depends on the value of the variable(s) declared outside the function.
We can use this small script to illustrate it:
var factor = 7 val multiplier = (i:Int) => i * factor val a = multiplier(11) val b = multiplier(12)
We define a function named multiplier
. The function expects an integer argument. For each argument, we take the argument and multiply it by the external variable factor.
We see this result: