June 2018
Intermediate to advanced
310 pages
6h 32m
English
Currying is a way to translate a function that takes a number of arguments into a chain of functions that each take a single argument. This may sound confusing, so let's look at a simple example:
fun subtract(x: Int, y: Int): Int { return x - y}println(subtract(50, 8))
This is a function that returns two arguments. The result is quite obvious. But maybe we would like to invoke this function with the following syntax instead:
subtract(50)(8)
We've already seen how we can return a function from another function:
fun subtract(x: Int): (Int) -> Int { return fun(y: Int): Int { return x - y }}
Here it is in the shorter form:
fun subtract(x: Int) = fun(y: Int): Int { return x + y}
And here it is in an even shorter form:
fun subtract(x: ...
Read now
Unlock full access