June 2018
Intermediate to advanced
316 pages
6h 34m
English
Function composition is a feature of functional programming that allows you to write simple and reliable code with significant performance improvements as compared with chain of function's invocations.
To avoid code such as this:
students .filter(::ageMoreThan20) .filter(::firstNameStartsWithE) .filter(::theLengthOfSecondNameMoreThan5)
You can write something like this:
fun predicate(student: Student) = ageMoreThan20(student) && firstNameStartsWithE(student) && theLengthOfSecondNameMoreThan5(student)students.filter(::predicate)
Or you can create your function to implement function composition. This function may look like this:
inline infix fun <P> ((P) -> Boolean).and(crossinline predicate: (P) -> Boolean): (P) -> ...
Read now
Unlock full access