discardLast

discardLast

  • (num: number) => (sequence: Sequence) => Sequence
  1. Discard the last num values

    const discardLast2 = discardLast(2)
    
    discardLast2(['a', 'b', 'c', 'd']) // is ['a', 'b']
    discardLast2('abcd') // is 'ab'
    const discardLast2 = discardLast(2)
    
    discardLast2(['a', 'b', 'c', 'd']) // is ['a', 'b']
    discardLast2('abcd') // is 'ab'
    
    const discardLast2 = discardLast(2)
    
    discardLast2(['a', 'b', 'c', 'd']) // is ['a', 'b']
    discardLast2('abcd') // is 'ab'
    const discardLast2 = discardLast(2)
    
    discardLast2(['a', 'b', 'c', 'd']) // is ['a', 'b']
    discardLast2('abcd') // is 'ab'
    
  2. Sometimes we have sorted data and don't need elements from the end. Below, we are starting a game. We're rolling to see our character's strength by casting four dice and removing the lowest two. Let's find our strength!

    const rolls = [1, 6, 2, 4]
    
    const orderDesc = order(withNumbersDescending)
    const removeTwoLowestRolls = discardLast(2)
    
    const getCharacterStat = compose([orderDesc, removeTwoLowestRolls, sumValues])
    
    const strength = getCharacterStat(rolls)
    console.log(strength) // is 10
    const rolls = [1, 6, 2, 4]
    
    const orderDesc = order(withNumbersDescending)
    const removeTwoLowestRolls = discardLast(2)
    
    const getCharacterStat = compose([orderDesc, removeTwoLowestRolls, sumValues])
    
    const strength = getCharacterStat(rolls)
    console.log(strength) // is 10
    
    const rolls = [1, 6, 2, 4]
    
    const orderDesc = order(withNumbersDescending)
    const removeTwoLowestRolls = discardLast(2)
    
    const getCharacterStat = compose([
      orderDesc,
      removeTwoLowestRolls,
      sumValues,
    ])
    
    const strength = getCharacterStat(rolls)
    console.log(strength) // is 10
    const rolls = [1, 6, 2, 4]
    
    const orderDesc = order(withNumbersDescending)
    const removeTwoLowestRolls = discardLast(2)
    
    const getCharacterStat = compose([
      orderDesc,
      removeTwoLowestRolls,
      sumValues,
    ])
    
    const strength = getCharacterStat(rolls)
    console.log(strength) // is 10