all

all

  • (predicate: Predicate) => (collection: Collection) => boolean
  1. Test if all entries return truthy

    const gt2 = n => n > 2
    const allGt2 = all(gt2)
    
    allGt2([2, 3]) // is false
    allGt2([3, 4]) // is true
    allGt2({ b: 2, c: 3 }) // is false
    allGt2({ c: 3, d: 4 }) // is true
    const gt2 = (n: number) => n > 2
    const allGt2 = all(gt2)
    
    allGt2([2, 3]) // is false
    allGt2([3, 4]) // is true
    allGt2({ b: 2, c: 3 }) // is false
    allGt2({ c: 3, d: 4 }) // is true
    
    const gt2 = n => n > 2
    const allGt2 = all(gt2)
    
    allGt2([2, 3]) // is false
    allGt2([3, 4]) // is true
    allGt2({ b: 2, c: 3 }) // is false
    allGt2({ c: 3, d: 4 }) // is true
    const gt2 = (n: number) => n > 2
    const allGt2 = all(gt2)
    
    allGt2([2, 3]) // is false
    allGt2([3, 4]) // is true
    allGt2({ b: 2, c: 3 }) // is false
    allGt2({ c: 3, d: 4 }) // is true
    
  2. We often want to know if all entries pass a condition. Below, a classroom earns a pizza party if all students score at least a B. Let's see which classrooms get pizza.

    const classroomScores = {
      A: [92, 98, 73],
      B: [95, 83, 88],
    }
    
    const gte80 = n => n >= 80
    const earnsParty = all(gte80)
    
    const getRoomsEarningParty = compose([keepWhen(earnsParty), Object.keys])
    const rooms = getRoomsEarningParty(classroomScores)
    console.log(rooms) // is [B]
    type ClassroomScores = Record<string, number[]>
    
    const classroomScores: ClassroomScores = {
      A: [92, 98, 73],
      B: [95, 83, 88],
    }
    
    const gte80 = (n: number) => n >= 80
    const earnsParty = all(gte80)<number[]>
    
    const getRoomsEarningParty = compose([
      keepWhen(earnsParty)<ClassroomScores>,
      Object.keys,
    ])
    const rooms = getRoomsEarningParty(classroomScores)
    console.log(rooms) // is [B]
    
    const classroomScores = {
      A: [92, 98, 73],
      B: [95, 83, 88],
    }
    
    const gte80 = n => n >= 80
    const earnsParty = all(gte80)
    
    const getRoomsEarningParty = compose([
      keepWhen(earnsParty),
      Object.keys,
    ])
    const rooms = getRoomsEarningParty(
      classroomScores
    )
    console.log(rooms) // is [B]
    type ClassroomScores = Record<string, number[]>
    
    const classroomScores: ClassroomScores = {
      A: [92, 98, 73],
      B: [95, 83, 88],
    }
    
    const gte80 = (n: number) => n >= 80
    const earnsParty = all(gte80)<number[]>
    
    const getRoomsEarningParty = compose([
      keepWhen(earnsParty)<ClassroomScores>,
      Object.keys,
    ])
    const rooms = getRoomsEarningParty(
      classroomScores
    )
    console.log(rooms) // is [B]