fillWith

fillWith

  • (value: any) => (collection: EntryCollection) => EntryCollection
  1. Fill a collection with value

    const zeroOut = fillWith(0)
    
    zeroOut([1, 2, 3]) // is [0, 0, 0]
    zeroOut({ a: 1, b: 2 }) // is { a: 0, b: 0 }
    const zeroOut = fillWith(0)
    
    zeroOut([1, 2, 3]) // is [0, 0, 0]
    zeroOut({ a: 1, b: 2 }) // is { a: 0, b: 0 }
    
    const zeroOut = fillWith(0)
    
    zeroOut([1, 2, 3]) // is [0, 0, 0]
    zeroOut({ a: 1, b: 2 }) // is { a: 0, b: 0 }
    const zeroOut = fillWith(0)
    
    zeroOut([1, 2, 3]) // is [0, 0, 0]
    zeroOut({ a: 1, b: 2 }) // is { a: 0, b: 0 }
    
  2. Sometimes we want to initialize a collection to a value. Below, we have the money per player in a game of Monopoly. They decide to restart the game since Jen is so far ahead. Let's reset everyone's money.

    const playerMoney = {
      grace: 180,
      sam: 350,
      jen: 2170,
    }
    
    const resetGame = fillWith(1500)
    const initialMoney = resetGame(playerMoney)
    console.log(initialMoney)
    // is
    // {
    //   grace: 1500,
    //   sam: 1500,
    //   jen: 1500,
    // }
    const playerMoney = {
      grace: 180,
      sam: 350,
      jen: 2170,
    }
    
    const resetGame = fillWith(1500)
    const initialMoney = resetGame(playerMoney)
    console.log(initialMoney)
    // is
    // {
    //   grace: 1500,
    //   sam: 1500,
    //   jen: 1500,
    // }
    
    const playerMoney = {
      grace: 180,
      sam: 350,
      jen: 2170,
    }
    
    const resetGame = fillWith(1500)
    const initialMoney = resetGame(playerMoney)
    console.log(initialMoney)
    // is
    // {
    //   grace: 1500,
    //   sam: 1500,
    //   jen: 1500,
    // }
    const playerMoney = {
      grace: 180,
      sam: 350,
      jen: 2170,
    }
    
    const resetGame = fillWith(1500)
    const initialMoney = resetGame(playerMoney)
    console.log(initialMoney)
    // is
    // {
    //   grace: 1500,
    //   sam: 1500,
    //   jen: 1500,
    // }