I am preparing a presentation on Scala and functional programming and I am not sure about two notions.
I use a function introduced earlier during the presentation:
def safe_division(x: Int, y: Int) : Option[Double] = {
if(y != 0)
Some(x / y.toDouble)
else
None
}
I created a curried version (please correct me if I am wrong!):
val curried_safe_division: (Int) => (Int) => Option[Double] = {
(x) =>
(y) =>
if(y != 0)
Some(x / y.toDouble)
else
None
}
So the first part where I am unsure is "is curried_safe_division called the curry?"
Then I introduce some code to show how currying functions allows a programmer to reuse functionalities efficiently:
val divideSix = curried_safe_division(6)
divideSix(3)
// prints: Some(2.0)
divideSix(6)
// prints: Some(1.0)
Am I right saying here that divideSix
is a closure?
Is curried_safe_division
not a closure as well?
I am using this definition:
https://softwareengineering.stackexchange.com/a/40708 a function that can be stored as a variable (referred to as a "first-class function"), that has a special ability to access other variables local to the scope it was created in.
I read multiple resources online, the wikipedia pages and this stackoverflow question: What is a 'Closure'? but it is still not super clear