Scala closures

A closure is a function. The function resultant value depends on the value of variable(s) declared outside the function.

We can use this small script by way of illustration:

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 Int argument. For each argument, we take the argument and multiply it by the external factor variable.

We see the following result:

Closures have a nice feel as they do what you expect with little fanfare.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.109.30