Search code examples
kotlinfold

Apply logical AND to a list of boolean values


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?


Solution

  • Assuming the list is not empty, if any item of the list is false then the result is false:

    val result = !list.any { !it }