-
Notifications
You must be signed in to change notification settings - Fork 72
Currying
Yanis Batura edited this page Jan 29, 2018
·
4 revisions
Currying transforms a function that takes multiple arguments and returns it as a chain of functions.
import org.funktionale.currying.*
val sum2ints = {(x: Int, y: Int)-> x + y }
val curried:(Int) -> (Int) -> Int = sum2ints.curried()
assertEquals(curried(2)(4), 6)
val add5 = curried(5)
assertEquals(add5(7), 12)
Also you could uncurry functions:
val sum2ints: (Int, Int) -> Int = { x, y -> x + y }
val curried: (Int) -> (Int) -> Int = sum2ints.curried()
assertEquals(curried(2)(4), 6)
//same type as sum2ints,
assertEquals(curried.uncurried()(2, 4), 6)
assertEquals(sum2ints(2, 4), 6)
val sum3ints: (Int, Int, Int) -> Int = { x, y, z -> x + y }
val f: (Int) -> (Int) -> (Int) -> Int =sum3ints.curried()
val x: (Int, Int, Int) -> Int =f.uncurried()