mPrependAll

mPrependAll

  • (prepended: array) => (base: array) => base
  1. Prepend the contents of one array ahead of another

    Consider prependAll instead.
    This is for the uncommon case where mutating base is required.

    const arr = ['c']
    const prependAB = mPrependAll(['a', 'b'])
    const mutatedArr = prependAB(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    const arr = ['c']
    const prependAB = mPrependAll(['a', 'b'])
    const mutatedArr = prependAB(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    
    const arr = ['c']
    const prependAB = mPrependAll(['a', 'b'])
    const mutatedArr = prependAB(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    const arr = ['c']
    const prependAB = mPrependAll(['a', 'b'])
    const mutatedArr = prependAB(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['a', 'b', 'c']
    
  2. Sometimes we want to mutate an array by prepending values ahead of it. Below, Sam is next in line for a roller coaster. Mike and Luke bought fast passes and want to join the line. Let's move them ahead of Sam.

    const queue = ['sam', 'jen']
    
    const cutInLine = mPrependAll(['mike', 'luke'])
    cutInLine(queue)
    console.log(queue)
    // is ['mike', 'luke', 'sam', 'jen']
    const queue = ['sam', 'jen']
    
    const cutInLine = mPrependAll(['mike', 'luke'])
    cutInLine(queue)
    console.log(queue)
    // is ['mike', 'luke', 'sam', 'jen']
    
    const queue = ['sam', 'jen']
    
    const cutInLine = mPrependAll(['mike', 'luke'])
    cutInLine(queue)
    console.log(queue)
    // is ['mike', 'luke', 'sam', 'jen']
    const queue = ['sam', 'jen']
    
    const cutInLine = mPrependAll(['mike', 'luke'])
    cutInLine(queue)
    console.log(queue)
    // is ['mike', 'luke', 'sam', 'jen']