Logical complement

A logical complement takes any predicate (a function with a return Boolean type) and negates it. Let's look at the following code:

import arrow.core.Predicate
import arrow.syntax.function.complement


fun main(args: Array<String>) {
val evenPredicate: Predicate<Int> = { i: Int -> i % 2 == 0 }
val oddPredicate: (Int) -> Boolean = evenPredicate.complement()

val numbers: IntRange = 1..10
val evenNumbers: List<Int> = numbers.filter(evenPredicate)
val oddNumbers: List<Int> = numbers.filter(oddPredicate)

println(evenNumbers)
println(oddNumbers)
}

Notice that we use a Predicate<T> type, but it is just an alias for (T) -> Boolean. There are complement extension functions for predicates from 0 to 22 parameters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.59