Tutorial by Examples

def multiply(factor: Int)(numberToBeMultiplied: Int): Int = factor * numberToBeMultiplied val multiplyBy3 = multiply(3)_ // resulting function signature Int => Int val multiplyBy10 = multiply(10)_ // resulting function signature Int => Int val sixFromCurriedCall = multiplyBy3(2) //6...
def numberOrCharacterSwitch(toggleNumber: Boolean)(number: Int)(character: Char): String = if (toggleNumber) number.toString else character.toString // need to explicitly specify the type of the parameter to be curried // resulting function signature Boolean => String val switchBetween3A...
def minus(left: Int, right: Int) = left - right val numberMinus5 = minus(_: Int, 5) val fiveMinusNumber = minus(5, _: Int) numberMinus5(7) // 2 fiveMinusNumber(7) // -2
Let's define a function of 2 arguments: def add: (Int, Int) => Int = (x,y) => x + y val three = add(1,2) Currying add transforms it into a function that takes one Int and returns a function (from one Int to an Int) val addCurried: (Int) => (Int => Int) = add2.curried // ...
Currying, according to Wikipedia, is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions. Concretely, in terms of scala types, in the context of a function that take two arguments, (has arity 2) it is the conversion o...
Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. This is normally useful when for example: different arguments of a function are calculated at different times. (Example 1) di...
What we have is a list of credit cards and we'd like to calculate the premiums for all those cards that the credit card company has to pay out. The premiums themselves depend on the total number of credit cards, so that the company adjust them accordingly. We already have a function that calculate...

Page 1 of 1