subtract

subtract

  • (right: number) => (left: number) => number
  1. Subtract one number from another

    const subtract1From = subtract(1)
    subtract1From(3) // is 2
    const subtract1From = subtract(1)
    subtract1From(3) // is 2
    
    const subtract1From = subtract(1)
    subtract1From(3) // is 2
    const subtract1From = subtract(1)
    subtract1From(3) // is 2
    
  2. Sometimes we want to subtract a constant. Below, we're building a sword for our game, which attacks for five health. Let's test it on a zombie.

    const sword = {
      attack: update({ health: subtract(5) }),
    }
    
    const enemy = {
      type: 'zombie',
      health: 15,
    }
    
    const woundedEnemy = sword.attack(enemy)
    console.log(woundedEnemy)
    // is {
    //   type: zombie
    //   health: 10
    // }
    type Enemy = {
      type: string
      health: number
    }
    const enemy: Enemy = {
      type: 'zombie',
      health: 15,
    }
    
    const sword = {
      attack: update({ health: subtract(5) })<Enemy>,
    }
    
    const woundedEnemy = sword.attack(enemy)
    console.log(woundedEnemy)
    // is {
    //   type: zombie
    //   health: 10
    // }
    
    const sword = {
      attack: update({ health: subtract(5) }),
    }
    
    const enemy = {
      type: 'zombie',
      health: 15,
    }
    
    const woundedEnemy = sword.attack(enemy)
    console.log(woundedEnemy)
    // is {
    //   type: zombie
    //   health: 10
    // }
    type Enemy = {
      type: string
      health: number
    }
    const enemy: Enemy = {
      type: 'zombie',
      health: 15,
    }
    
    const sword = {
      attack: update({ health: subtract(5) })<Enemy>,
    }
    
    const woundedEnemy = sword.attack(enemy)
    console.log(woundedEnemy)
    // is {
    //   type: zombie
    //   health: 10
    // }