September 2018
Intermediate to advanced
398 pages
9h 43m
English
In practice, there is no need to call .curried to define curried functions. You can declare curried functions directly with multiple parameter lists. Here is a curried function that calculates a discount to an Item class:
case class Item(description: String, price: Double)def discount(percentage: Double)(item: Item): Item = item.copy(price = item.price * (1 - percentage / 100))
We can fully apply the function if we provide two argument lists, as follows:
discount(10)(Item("Monitor", 500))// res6: Item = Item(Monitor,450.0)
But we can also partially apply the function if we just provide the first argument list and add a _ character to indicate that we want a function value, as shown in the following code:
val Read now
Unlock full access