July 2017
Intermediate to advanced
796 pages
18h 55m
English
Sometimes in your code, you don't want to define a function prior to its usage, maybe because you will use it in one place. In functional programming, there's a type of function that is very suitable to this situation. It's called an anonymous function. Let's demonstrate the use of anonymous functions using the previous example of transferring money:
def TransferMoney(money: Double, bankFee: Double => Double): Double = { money + bankFee(money)}
Now, let's call the TransferMoney() method with some real value as follows:
TransferMoney(100, (amount: Double) => amount * 0.05)
Read now
Unlock full access