Consider a mutable list with boolean values,
MutableList{true, false, false}
How to return the boolean value after performing a logical AND on all of the values within the list using Kotlin fold?
This is a common case for fold operator, you mentioned.
val list = mutableListOf(true, false, false)
val result = list.fold(initial = true) { accumulator, nextItem ->
accumulator && nextItem
}
alternatively you can just check if list contains even one false using:
val result = (false !in list)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With