Let’s see how we can wire up functions with & without currying, and then touch on functional composition. Without currying Here are simple functions example that add, add10, multiply and multiplyBy10 without currying.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
package com.sbthello object MyApp extends App { val input = 5 println("input=" + input) def add(x: Int, y: Int) = { x + y } def add10(x: Int) = add(x, 10) def multiply(x: Int, y: Int) = { x * y } def multiplyBy10(x: Int) = multiply(x, 10) println("add10=" + add10(input)) println("multiplyBy10=" + multiplyBy10(input)) //wiring def addAndThenMultiply(x: Int) = { multiplyBy10(add10(x)) } println("add10AndThenmultiplyBy10=" + addAndThenMultiply(input)); } |
Output:
|
|
input=5 add10=15 multiplyBy10=50 add10AndThenmultiplyBy10=150 |
With currying &… Read more ›...