splitEvery

splitEvery

  • (groupSize: number) => (sequence: Sequence) => Sequence[]
  1. Split the sequence into groups of a size.

    const splitEvery2 = splitEvery(2)
    
    splitEvery2('abc') // is ['ab', 'c']
    splitEvery2(['a', 'b', 'c']) // is [['a', 'b'], ['c']]
    
    splitEvery2('') // is ['']
    splitEvery2([]) // is [[]]
    const splitEvery2 = splitEvery(2)
    
    splitEvery2('abc') // is ['ab', 'c']
    splitEvery2(['a', 'b', 'c']) // is [['a', 'b'], ['c']]
    
    splitEvery2('') // is ['']
    splitEvery2([]) // is [[]]
    
    const splitEvery2 = splitEvery(2)
    
    splitEvery2('abc') // is ['ab', 'c']
    splitEvery2(['a', 'b', 'c']) // is [['a', 'b'], ['c']]
    
    splitEvery2('') // is ['']
    splitEvery2([]) // is [[]]
    const splitEvery2 = splitEvery(2)
    
    splitEvery2('abc') // is ['ab', 'c']
    splitEvery2(['a', 'b', 'c']) // is [['a', 'b'], ['c']]
    
    splitEvery2('') // is ['']
    splitEvery2([]) // is [[]]
    
  2. Sometimes we want to split a sequence into groups of a size. Below, we have a list of students. We're splitting them up into groups of 3 for a class project. Let's make the groups.

    const students = ['mike', 'luke', 'emma', 'meg', 'tom']
    const createGroupsOf3 = compose([shuffle, splitEvery(3)])
    
    const groups = createGroupsOf3(students)
    console.log(groups)
    // is [
    //   [group of 3]
    //   [group of 2]
    // ]
    const students = ['mike', 'luke', 'emma', 'meg', 'tom']
    const createGroupsOf3 = compose([shuffle<string>, splitEvery(3)<string[]>])
    
    const groups = createGroupsOf3(students)
    console.log(groups)
    // is [
    //   [group of 3]
    //   [group of 2]
    // ]
    
    const students = [
      'mike',
      'luke',
      'emma',
      'meg',
      'tom',
    ]
    const createGroupsOf3 = compose([
      shuffle,
      splitEvery(3),
    ])
    
    const groups = createGroupsOf3(students)
    console.log(groups)
    // is [
    //   [group of 3]
    //   [group of 2]
    // ]
    const students = [
      'mike',
      'luke',
      'emma',
      'meg',
      'tom',
    ]
    const createGroupsOf3 = compose([
      shuffle<string>,
      splitEvery(3)<string[]>,
    ])
    
    const groups = createGroupsOf3(students)
    console.log(groups)
    // is [
    //   [group of 3]
    //   [group of 2]
    // ]