Task 3 – Monad usage
Given a sequence of operations needed to upgrade a tower—FetchTower
, UpgradeTower
, and DeployTower
—with each potentially failing and returning Result<Tower, string>
, use the monad concept to chain these operations together for a given tower ID. Ensure that if any step fails, the entire operation short-circuits and returns the error:
public Result<Tower, string> FetchTower(int towerId) { /* Fetches tower based on ID */ } public Result<Tower, string> UpgradeTower(Tower tower) { /* Upgrades the tower and can fail */ } public Result<Tower, string> DeployTower(Tower tower) { /* Attempts to deploy the tower */ }
If you successfully wrote the solutions for all three tasks, you are awesome! If you struggle or don’t know how to solve the tasks now, don’t worry, you will be awesome after completing this chapter and solving them.