mAppendOne

mAppendOne

  • (value: any) => (anArray: array) => anArray
  1. Push a value onto an array

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

    const arr = ['a', 'b']
    const pushC = mAppendOne('c')
    const mutatedArr = pushC(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    const arr = ['a', 'b']
    const pushC = mAppendOne('c')
    const mutatedArr = pushC(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    
    const arr = ['a', 'b']
    const pushC = mAppendOne('c')
    const mutatedArr = pushC(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    const arr = ['a', 'b']
    const pushC = mAppendOne('c')
    const mutatedArr = pushC(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    
  2. Sometimes we want to mutate an array by pushing a value onto it. Below, our fast food joint has a super kitty promotion where every order gets a plushy. Let's add one to each order.

    const orders = {
      mike: ['burger', 'fries'],
      luke: ['chicken sandwich', 'soda'],
    }
    
    const addSuperKitty = mAppendOne('super kitty')
    const addSuperKitties = forEach(addSuperKitty)
    addSuperKitties(orders)
    console.log(orders)
    // is {
    //   mike: [burger, fries, super kitty]
    //   luke: [chicken sandwich, soda, super kitty]
    // }
    type Orders = Record<string, string[]>
    const orders: Orders = {
      mike: ['burger', 'fries'],
      luke: ['chicken sandwich', 'soda'],
    }
    
    const addSuperKitty = mAppendOne('super kitty')<string[]>
    const addSuperKitties = forEach(addSuperKitty)<Orders>
    addSuperKitties(orders)
    console.log(orders)
    // is {
    //   mike: [burger, fries, super kitty]
    //   luke: [chicken sandwich, soda, super kitty]
    // }
    
    const orders = {
      mike: ['burger', 'fries'],
      luke: ['chicken sandwich', 'soda'],
    }
    
    const addSuperKitty = mAppendOne('super kitty')
    const addSuperKitties = forEach(addSuperKitty)
    addSuperKitties(orders)
    console.log(orders)
    // is {
    //   mike: [burger, fries, super kitty]
    //   luke: [chicken sandwich, soda, super kitty]
    // }
    type Orders = Record<string, string[]>
    const orders: Orders = {
      mike: ['burger', 'fries'],
      luke: ['chicken sandwich', 'soda'],
    }
    
    const addSuperKitty = mAppendOne('super kitty')<
      string[]
    >
    const addSuperKitties = forEach(
      addSuperKitty
    )<Orders>
    addSuperKitties(orders)
    console.log(orders)
    // is {
    //   mike: [burger, fries, super kitty]
    //   luke: [chicken sandwich, soda, super kitty]
    // }