Scala partial application unclear
partial-application, scala
Solution
You can explicitly return a function from `myOperation`:
def myOperation(x: Int): Int => Int = {
val complexVal = complexCalc(x)
println("complexVal calculated")
(y: Int) => complexVal + y
}
Problem
I don't have very clear the partial application of functions in Scala... I will do an example: ``` def myOperation(x: Int)(y: Int): Int = { val complexVal = complexCalc(x) println("complexVal calculated") complexVal + y } def complexCalc(x: Int): Int = x * 2 val partial = myOperation(5)_ println("calculate") println("result(3): " + partial(3)) println("result(1): " + partial(1)) ``` The output of this will be: ``` calculate complexVal calculated result(3): 13 complexVal calculated result(1): 11 ``` So the `complexVal` was calculated 2 times, what if I want to calculate it just once? For who has javascript knowledge something like: ``` function myOperation(x) { var complexVal = complexCalc(x) return function(y){ complexVal + y } } ``` EDIT: So what's the difference between what I've written previously and this: ``` def myOperation2(x: Int, y: Int): Int = { val complexVal = complexCalculation(x) println("complexVal calculated") complexVal + y } val partial = myOperation(5)_ val partial2 = myOperation2(5, _: Int) ```