Solutions
Here are the solutions to the exercises related to currying and partial application within the context of a mobile tower defense game. These solutions demonstrate how to implement the concepts discussed and provide practical examples of their use in game development.
Solution 1
To refactor the AttackEnemy
function using currying, we first define the original function and then transform it:
public Func<int, int, void> CurriedAttack(TowerTypes towerType) { return (enemyId, damage) => { Console.WriteLine($"Tower {towerType} attacks enemy {enemyId} for {damage} damage."); }; } var attackWithCannon = CurriedAttack(TowerTypes.Cannon); attackWithCannon(1, 50); // Attack enemy 1 with 50 damage attackWithCannon(2, 75); // Attack enemy 2 with 75 damage
This curried function allows the towerType
to be set once and reused for multiple attacks, enhancing code reusability...