Exercises
In this chapter, we’ve explored the functional programming concepts of currying and partial application. Now, let’s apply these techniques to practical scenarios in a mobile tower defense game.
Exercise 1
Refactor the AttackEnemy
function using currying, allowing the towerType
to be preset for multiple uses throughout the game while accepting dynamic inputs for enemyId
and damage
:
public void AttackEnemy(TowerTypes towerType, int enemyId, int damage) { Console.WriteLine($"Tower {towerType} attacks enemy {enemyId} for {damage} damage."); }
Exercise 2
Apply partial application to create a function for quick setup of standard game settings where the map
is predefined but allows for dynamic setting of difficultyLevel
and isMultiplayer
:
public void SetGameSettings(string map, int difficultyLevel, bool isMultiplayer) { Console.WriteLine($"Setting game on map {map} with difficulty {difficultyLevel...