mDiscardRange

mDiscardRange

  • (range: Range) => (anArray: array) => anArray
  1. Discard a range of elements

    Ranges in Common FP are inclusive

    Consider discardRange instead.
    This is for the uncommon case where mutating anArray is required.

    const range = { startIdx: 1, endIdx: 2 }
    const mDiscard2ndAnd3rd = mDiscardRange(range)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mDiscard2ndAnd3rd(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [1, 4]
    const range = { startIdx: 1, endIdx: 2 }
    const mDiscard2ndAnd3rd = mDiscardRange(range)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mDiscard2ndAnd3rd(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [1, 4]
    
    const range = { startIdx: 1, endIdx: 2 }
    const mDiscard2ndAnd3rd = mDiscardRange(range)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mDiscard2ndAnd3rd(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [1, 4]
    const range = { startIdx: 1, endIdx: 2 }
    const mDiscard2ndAnd3rd = mDiscardRange(range)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mDiscard2ndAnd3rd(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [1, 4]
    
  2. Sometimes we want to remove a range of elements. Below, we track the number of page visits to our site. Unfortunately, a bot generated an abnormally high number of visits for two hours. We stopped the bot; now let's remove those visits and calculate our hourly average.

    const hourlyVisitors = [108, 125, 149, 163, 8692, 9154, 312, 356]
    
    const hours5and6 = { startIdx: 4, endIdx: 5 }
    const removeBotVisitors = mDiscardRange(hours5and6)
    removeBotVisitors(hourlyVisitors)
    
    const getAverageVisitors = compose([getAverageValue, roundToNearest('1')])
    
    const hourlyAverage = getAverageVisitors(hourlyVisitors)
    console.log(hourlyAverage) // is 202
    const hourlyVisitors = [108, 125, 149, 163, 8692, 9154, 312, 356]
    
    const hours5and6 = { startIdx: 4, endIdx: 5 }
    const removeBotVisitors = mDiscardRange(hours5and6)
    removeBotVisitors(hourlyVisitors)
    
    const getAverageVisitors = compose([
      getAverageValue<number[]>,
      roundToNearest('1'),
    ])
    
    const hourlyAverage = getAverageVisitors(hourlyVisitors)
    console.log(hourlyAverage) // is 202
    
    const hourlyVisitors = [
      108, 125, 149, 163, 8692, 9154, 312, 356,
    ]
    
    const hours5and6 = { startIdx: 4, endIdx: 5 }
    const removeBotVisitors =
      mDiscardRange(hours5and6)
    removeBotVisitors(hourlyVisitors)
    
    const getAverageVisitors = compose([
      getAverageValue,
      roundToNearest('1'),
    ])
    
    const hourlyAverage = getAverageVisitors(
      hourlyVisitors
    )
    console.log(hourlyAverage) // is 202
    const hourlyVisitors = [
      108, 125, 149, 163, 8692, 9154, 312, 356,
    ]
    
    const hours5and6 = { startIdx: 4, endIdx: 5 }
    const removeBotVisitors =
      mDiscardRange(hours5and6)
    removeBotVisitors(hourlyVisitors)
    
    const getAverageVisitors = compose([
      getAverageValue<number[]>,
      roundToNearest('1'),
    ])
    
    const hourlyAverage = getAverageVisitors(
      hourlyVisitors
    )
    console.log(hourlyAverage) // is 202