randomlyKeep

randomlyKeep

  • (num: number) => (collection: Collection) => Collection
  1. Randomly pick num entries.

    const pick2 = randomlyKeep(2)
    
    pick2(['a', 'b', 'c', 'd']) // is 2 random elements
    pick2(['a']) // is ['a']
    const pick2 = randomlyKeep(2)
    
    pick2(['a', 'b', 'c', 'd']) // is 2 random elements
    pick2(['a']) // is ['a']
    
    const pick2 = randomlyKeep(2)
    
    pick2(['a', 'b', 'c', 'd']) // is 2 random elements
    pick2(['a']) // is ['a']
    const pick2 = randomlyKeep(2)
    
    pick2(['a', 'b', 'c', 'd']) // is 2 random elements
    pick2(['a']) // is ['a']
    
  2. Sometimes we want to randomly pick items. Below, we have a classroom with a dirty chalkboard. Every day the teacher randomly picks two students to clean it. Let's pick them.

    const students = ['tom', 'ken', 'chris', 'liz']
    
    const pick2 = randomlyKeep(2)
    const chalkboardCleaners = pick2(students)
    console.log(chalkboardCleaners)
    // is two random students
    const students = ['tom', 'ken', 'chris', 'liz']
    
    const pick2 = randomlyKeep(2)<string[]>
    const chalkboardCleaners = pick2(students)
    console.log(chalkboardCleaners)
    // is two random students
    
    const students = ['tom', 'ken', 'chris', 'liz']
    
    const pick2 = randomlyKeep(2)
    const chalkboardCleaners = pick2(students)
    console.log(chalkboardCleaners)
    // is two random students
    const students = ['tom', 'ken', 'chris', 'liz']
    
    const pick2 = randomlyKeep(2)<string[]>
    const chalkboardCleaners = pick2(students)
    console.log(chalkboardCleaners)
    // is two random students