September 2019
Intermediate to advanced
462 pages
11h 3m
English
Functions are objects in Kotlin, and you can inject methods into functions, like you can inject methods into classes. In Java 8, the functional interface Function<T, R> has an andThen() method to combine two functions, and we can use this to compose operations—see Functional Programming in Java [Sub14]. Kotlin’s Function doesn’t have that method, but we can inject an andThen() method into Kotlin functions like this, for example:
| | fun <T, R, U> ((T) -> R).andThen(next: (R) -> U): (T) -> U = |
| | { input: T -> next(this(input)) } |
The extension function signature says that andThen() is added to a function that takes a parametrized type T and returns a result of type R. The parameter passed to ...
Read now
Unlock full access