mKeepRange

mKeepRange

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

    Ranges in Common FP are inclusive

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

    const range = { startIdx: 1, endIdx: 2 }
    const mKeep2ndAnd3rd = mKeepRange(range)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeep2ndAnd3rd(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [2, 3]
    const range = { startIdx: 1, endIdx: 2 }
    const mKeep2ndAnd3rd = mKeepRange(range)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeep2ndAnd3rd(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [2, 3]
    
    const range = { startIdx: 1, endIdx: 2 }
    const mKeep2ndAnd3rd = mKeepRange(range)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeep2ndAnd3rd(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [2, 3]
    const range = { startIdx: 1, endIdx: 2 }
    const mKeep2ndAnd3rd = mKeepRange(range)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeep2ndAnd3rd(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [2, 3]
    
  2. Sometimes we want to keep a range of elements. Below, we have the NFL box scores for two Eagles receivers. Let's find their combined total receptions and yards.

    const statsByReciever = [
      ['receiver', 'targets', 'receptions', 'yards', 'touchdowns'],
      ['a.j. brown', 11, 8, 110, 1],
      ['devonta smith', 12, 11, 109, 1],
    ]
    
    const recAndYds = { startIdx: 2, endIdx: 3 }
    const keepRecAndYds = mKeepRange(recAndYds)
    const trim = forEach(keepRecAndYds)
    trim(statsByReciever)
    
    const statsByMetric = transpose(statsByReciever)
    const format = ([metric, ...stats]) => {
      const total = sumValues(stats)
      return `  ${metric}: ${total}`
    }
    const getTotals = compose([
      mapValues(format),
      prependOne('A.J. Brown and DeVonta Smith combined for'),
      joinValues('\n'),
    ])
    const totals = getTotals(statsByMetric)
    console.log(totals)
    // prints
    // A.J. Brown and DeVonta Smith combined for
    //   receptions: 19
    //   yards: 219
    type Row = Array<string | number>
    const statsByReceiver: Row[] = [
      ['receiver', 'targets', 'receptions', 'yards', 'touchdowns'],
      ['a.j. brown', 11, 8, 110, 1],
      ['devonta smith', 12, 11, 109, 1],
    ]
    
    const recAndYds = { startIdx: 2, endIdx: 3 }
    const keepRecAndYds = mKeepRange(recAndYds)<Row>
    const trim = forEach(keepRecAndYds)<Row[]>
    trim(statsByReceiver)
    
    type MetricStats = [string, ...number[]]
    const statsByMetric = transpose(statsByReceiver) as MetricStats[]
    
    const format = (metricStats: MetricStats) => {
      const [metric, ...stats] = metricStats
      const total = sumValues(stats)
      return `  ${metric}: ${total}`
    }
    const getTotals = compose([
      mapValues(format)<MetricStats[]>,
      prependOne('A.J. Brown and DeVonta Smith combined for'),
      joinValues('\n'),
    ])
    const totals = getTotals(statsByMetric)
    console.log(totals)
    // prints
    // A.J. Brown and DeVonta Smith combined for
    //  receptions: 19
    //  yards: 219
    
    const statsByReciever = [
      [
        'receiver',
        'targets',
        'receptions',
        'yards',
        'touchdowns',
      ],
      ['a.j. brown', 11, 8, 110, 1],
      ['devonta smith', 12, 11, 109, 1],
    ]
    
    const recAndYds = { startIdx: 2, endIdx: 3 }
    const keepRecAndYds = mKeepRange(recAndYds)
    const trim = forEach(keepRecAndYds)
    trim(statsByReciever)
    
    const statsByMetric = transpose(statsByReciever)
    const format = ([metric, ...stats]) => {
      const total = sumValues(stats)
      return `  ${metric}: ${total}`
    }
    const getTotals = compose([
      mapValues(format),
      prependOne(
        'A.J. Brown and DeVonta Smith combined for'
      ),
      joinValues('\n'),
    ])
    const totals = getTotals(statsByMetric)
    console.log(totals)
    // prints
    // A.J. Brown and DeVonta Smith combined for
    //   receptions: 19
    //   yards: 219
    type Row = Array<string | number>
    const statsByReceiver: Row[] = [
      [
        'receiver',
        'targets',
        'receptions',
        'yards',
        'touchdowns',
      ],
      ['a.j. brown', 11, 8, 110, 1],
      ['devonta smith', 12, 11, 109, 1],
    ]
    
    const recAndYds = { startIdx: 2, endIdx: 3 }
    const keepRecAndYds = mKeepRange(recAndYds)<Row>
    const trim = forEach(keepRecAndYds)<Row[]>
    trim(statsByReceiver)
    
    type MetricStats = [string, ...number[]]
    const statsByMetric = transpose(
      statsByReceiver
    ) as MetricStats[]
    
    const format = (metricStats: MetricStats) => {
      const [metric, ...stats] = metricStats
      const total = sumValues(stats)
      return `  ${metric}: ${total}`
    }
    const getTotals = compose([
      mapValues(format)<MetricStats[]>,
      prependOne(
        'A.J. Brown and DeVonta Smith combined for'
      ),
      joinValues('\n'),
    ])
    const totals = getTotals(statsByMetric)
    console.log(totals)
    // prints
    // A.J. Brown and DeVonta Smith combined for
    //  receptions: 19
    //  yards: 219