Search code examples
scalabooleanlogical-operatorsfold

Applying logical and to list of boolean values


Consider the following list of Boolean values in Scala

List(true, false, false, true)

How would you using either foldRight or foldLeft emulate the function of performing a logical AND on all of the values within the list?


Solution

  • val l = List(true, false, false, true)
    val andAll = l.foldLeft(true)(_ && _)