mKeepLastWhile

mKeepLastWhile

  • (predicate: Predicate) => (anArray: array) => anArray
  1. Keep the last elements that pass a condition

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

    const gt2 = n => n > 2
    const mKeepLastWhileLt3 = mKeepLastWhile(gt2)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeepLastWhileLt3(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    const gt2 = (n: number) => n > 2
    const mKeepLastWhileLt3 = mKeepLastWhile(gt2)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeepLastWhileLt3(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    
    const gt2 = n => n > 2
    const mKeepLastWhileLt3 = mKeepLastWhile(gt2)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeepLastWhileLt3(arr)
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is [3, 4]
    const gt2 = (n: number) => n > 2
    const mKeepLastWhileLt3 = mKeepLastWhile(gt2)
    
    const arr = [1, 2, 3, 4]
    const mutatedArr = mKeepLastWhileLt3(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 elements. Below, we run a game shop with limited shelf space. Every quarter, we rotate out the games with under 50 sales. Using our list of games sorted by number sold, let's find which ones to rotate out.

    const games = [
      { name: 'coup', numSold: 68 },
      { name: 'camel up', numSold: 52 },
      { name: 'battle sheep', numSold: 35 },
      { name: 'mastermind', numSold: 27 },
    ]
    
    const soldUnder50 = ({ numSold }) => numSold < 50
    const keepGamesToRotateOut = mKeepLastWhile(soldUnder50)
    keepGamesToRotateOut(games)
    const getNames = mapValues(get('name'))
    const gamesToRotateOut = getNames(games)
    console.log(gamesToRotateOut)
    // is [
    //   battle sheep
    //   mastermind
    // ]
    type Game = {
      name: string
      numSold: number
    }
    const games: Game[] = [
      { name: 'coup', numSold: 68 },
      { name: 'camel up', numSold: 52 },
      { name: 'battle sheep', numSold: 35 },
      { name: 'mastermind', numSold: 27 },
    ]
    
    const soldUnder50 = ({ numSold }: Game) => numSold < 50
    const keepGamesToRotateOut = mKeepLastWhile(soldUnder50)
    keepGamesToRotateOut(games)
    const getName = get('name')<Game>
    const getAllNames = mapValues(getName)<Game[]>
    const gamesToRotateOut = getAllNames(games)
    console.log(gamesToRotateOut)
    // is [
    //   battle sheep
    //   mastermind
    // ]
    
    const games = [
      { name: 'coup', numSold: 68 },
      { name: 'camel up', numSold: 52 },
      { name: 'battle sheep', numSold: 35 },
      { name: 'mastermind', numSold: 27 },
    ]
    
    const soldUnder50 = ({ numSold }) => numSold < 50
    const keepGamesToRotateOut =
      mKeepLastWhile(soldUnder50)
    keepGamesToRotateOut(games)
    const getNames = mapValues(get('name'))
    const gamesToRotateOut = getNames(games)
    console.log(gamesToRotateOut)
    // is [
    //   battle sheep
    //   mastermind
    // ]
    type Game = {
      name: string
      numSold: number
    }
    const games: Game[] = [
      { name: 'coup', numSold: 68 },
      { name: 'camel up', numSold: 52 },
      { name: 'battle sheep', numSold: 35 },
      { name: 'mastermind', numSold: 27 },
    ]
    
    const soldUnder50 = ({ numSold }: Game) =>
      numSold < 50
    const keepGamesToRotateOut =
      mKeepLastWhile(soldUnder50)
    keepGamesToRotateOut(games)
    const getName = get('name')<Game>
    const getAllNames = mapValues(getName)<Game[]>
    const gamesToRotateOut = getAllNames(games)
    console.log(gamesToRotateOut)
    // is [
    //   battle sheep
    //   mastermind
    // ]