mKeepLast

mKeepLast

  • (num: number) => (anArray: array) => anArray
  1. Keep the last num elements of an array

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

    const mKeepLast2 = mKeepLast(2)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeepLast2(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    const mKeepLast2 = mKeepLast(2)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeepLast2(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    
    const mKeepLast2 = mKeepLast(2)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeepLast2(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    const mKeepLast2 = mKeepLast(2)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeepLast2(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    
  2. Sometimes we have ordered data and only care about the last items. Below, we run a shop "Brooke's Books" and are preparing for our monthly meeting. Let's report our sales for the last two months.

    const salesByMonth = [
      ['month', 'subscriptions', 'books'],
      ['Jan', 129, 561],
      ['Feb', 98, 523],
      ['Mar', 112, 530],
      ['Apr', 107, 588],
    ]
    
    const keepMostRecent2Months = mKeepLast(2)
    keepMostRecent2Months(salesByMonth)
    
    const format = sales => {
      const [month, subsSold, booksSold] = sales
      return [
        month,
        `  subscriptions: ${subsSold}`,
        `  books:         ${booksSold}`,
      ].join('\n')
    }
    
    const createReport = compose([
      mapValues(format),
      prependOne('Sales From Last Two Months'),
      joinValues('\n'),
    ])
    const report = createReport(salesByMonth)
    
    console.log(report)
    // prints
    // Sales From Last Two Months
    // Mar
    //   subscriptions: 112
    //   books:         530
    // Apr
    //   subscriptions: 107
    //   books:         588
    type Row = Array<string | number>
    
    const salesByMonth: Row[] = [
      ['month', 'subscriptions', 'books'],
      ['Jan', 129, 561],
      ['Feb', 98, 523],
      ['Mar', 112, 530],
      ['Apr', 107, 588],
    ]
    
    const keepMostRecent2Months = mKeepLast(2)<Row[]>
    keepMostRecent2Months(salesByMonth)
    
    const format = (sales: Row) => {
      const [month, subsSold, booksSold] = sales
      return [
        month,
        `  subscriptions: ${subsSold}`,
        `  books:         ${booksSold}`,
      ].join('\n')
    }
    
    const createReport = compose([
      mapValues(format)<Row[]>,
      prependOne('Sales From Last Two Months'),
      joinValues('\n'),
    ])
    const report = createReport(salesByMonth)
    
    console.log(report)
    // prints
    // Sales From Last Two Months
    // Mar
    //   subscriptions: 112
    //   books:         530
    // Apr
    //   subscriptions: 107
    //   books:         588
    
    const salesByMonth = [
      ['month', 'subscriptions', 'books'],
      ['Jan', 129, 561],
      ['Feb', 98, 523],
      ['Mar', 112, 530],
      ['Apr', 107, 588],
    ]
    
    const keepMostRecent2Months = mKeepLast(2)
    keepMostRecent2Months(salesByMonth)
    
    const format = sales => {
      const [month, subsSold, booksSold] = sales
      return [
        month,
        `  subscriptions: ${subsSold}`,
        `  books:         ${booksSold}`,
      ].join('\n')
    }
    
    const createReport = compose([
      mapValues(format),
      prependOne('Sales From Last Two Months'),
      joinValues('\n'),
    ])
    const report = createReport(salesByMonth)
    
    console.log(report)
    // prints
    // Sales From Last Two Months
    // Mar
    //   subscriptions: 112
    //   books:         530
    // Apr
    //   subscriptions: 107
    //   books:         588
    type Row = Array<string | number>
    
    const salesByMonth: Row[] = [
      ['month', 'subscriptions', 'books'],
      ['Jan', 129, 561],
      ['Feb', 98, 523],
      ['Mar', 112, 530],
      ['Apr', 107, 588],
    ]
    
    const keepMostRecent2Months = mKeepLast(2)<Row[]>
    keepMostRecent2Months(salesByMonth)
    
    const format = (sales: Row) => {
      const [month, subsSold, booksSold] = sales
      return [
        month,
        `  subscriptions: ${subsSold}`,
        `  books:         ${booksSold}`,
      ].join('\n')
    }
    
    const createReport = compose([
      mapValues(format)<Row[]>,
      prependOne('Sales From Last Two Months'),
      joinValues('\n'),
    ])
    const report = createReport(salesByMonth)
    
    console.log(report)
    // prints
    // Sales From Last Two Months
    // Mar
    //   subscriptions: 112
    //   books:         530
    // Apr
    //   subscriptions: 107
    //   books:         588