Exercises
After explaining the concepts, Julia challenged Steve to apply what he’d learned.
Julia: Now that you understand the basics, why don’t you try refactoring your game’s upgrade system using monads? It should make your code more robust and easier to reason about.
Steve: That’s a great idea! I can already see how this could simplify some of my more complex game logic.
Exercise 1
Given a Result<List<Tower>, string>
type that represents a list of towers, where Tower
is a class containing properties such as Id
, Name
, and Damage
, the task is to use the functor concept to apply a function to each tower that appends “(Upgraded)” to the end of its name to indicate that the tower has been upgraded:
public class Tower { public int Id { get; set; } public string Name { get; set; } public int Damage { get; set; } } public Result<List<Tower>...