mDiscardFirst

mDiscardFirst

  • (num: number) => (anArray: array) => anArray
  1. Discard the first num values

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

    const mDiscardFirst2 = mDiscardFirst(2)
    const arr = [1, 2, 3, 4]
    const mutatedArr = mDiscardFirst2(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    const mDiscardFirst2 = mDiscardFirst(2)
    const arr = [1, 2, 3, 4]
    const mutatedArr = mDiscardFirst2(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    
    const mDiscardFirst2 = mDiscardFirst(2)
    const arr = [1, 2, 3, 4]
    const mutatedArr = mDiscardFirst2(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    const mDiscardFirst2 = mDiscardFirst(2)
    const arr = [1, 2, 3, 4]
    const mutatedArr = mDiscardFirst2(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    
  2. Sometimes we get data in the form of arrays rather than objects. Below, we have logs with redundant data. Specifically, we only have one server and one realm. Let's remove that data.

    const [_headers, ...logs] = [
      ['server', 'realm', 'time', 'message'],
      ['east3', 'api', '10:00:05', 'user purchased hat'],
      ['east3', 'api', '10:00:06', 'purchase passed audit'],
      ['east3', 'api', '10:00:08', 'error requesting customer data'],
    ]
    
    const removeServerAndRealm = mDiscardFirst(2)
    const trim = forEach(removeServerAndRealm)
    trim(logs)
    console.log(logs)
    // is [
    //  ['10:00:05', 'user purchased hat'],
    //  ['10:00:06', 'purchase passed audit'],
    //  ['10:00:08', 'error requesting customer data'],
    // ]
    const [_headers, ...logs] = [
      ['server', 'realm', 'timestamp', 'message'],
      ['east3', 'api', '10:00:05', 'user purchased hat'],
      ['east3', 'api', '10:00:06', 'purchase passed audit'],
      ['east3', 'api', '10:00:08', 'error requesting customer data'],
    ]
    
    const removeServerAndRealm = mDiscardFirst(2)<string[]>
    const trim = forEach(removeServerAndRealm)<string[][]>
    trim(logs)
    console.log(logs)
    // is [
    //  ['10:00:05', 'user purchased hat'],
    //  ['10:00:06', 'purchase passed audit'],
    //  ['10:00:08', 'error requesting customer data'],
    // ]
    
    const [_headers, ...logs] = [
      ['server', 'realm', 'time', 'message'],
      [
        'east3',
        'api',
        '10:00:05',
        'user purchased hat',
      ],
      [
        'east3',
        'api',
        '10:00:06',
        'purchase passed audit',
      ],
      [
        'east3',
        'api',
        '10:00:08',
        'error requesting customer data',
      ],
    ]
    
    const removeServerAndRealm = mDiscardFirst(2)
    const trim = forEach(removeServerAndRealm)
    trim(logs)
    console.log(logs)
    // is [
    //  ['10:00:05', 'user purchased hat'],
    //  ['10:00:06', 'purchase passed audit'],
    //  ['10:00:08', 'error requesting customer data'],
    // ]
    const [_headers, ...logs] = [
      ['server', 'realm', 'timestamp', 'message'],
      [
        'east3',
        'api',
        '10:00:05',
        'user purchased hat',
      ],
      [
        'east3',
        'api',
        '10:00:06',
        'purchase passed audit',
      ],
      [
        'east3',
        'api',
        '10:00:08',
        'error requesting customer data',
      ],
    ]
    
    const removeServerAndRealm = mDiscardFirst(2)<
      string[]
    >
    const trim = forEach(removeServerAndRealm)<
      string[][]
    >
    trim(logs)
    console.log(logs)
    // is [
    //  ['10:00:05', 'user purchased hat'],
    //  ['10:00:06', 'purchase passed audit'],
    //  ['10:00:08', 'error requesting customer data'],
    // ]