Using do-syntax
When working with anonymous functions, we may end up having a code block that is in the middle of a function call, making the code more difficult to read. The do-syntax is a great way to address this problem and produce clear, easy-to-read code.
To illustrate the concept, let's build up a new use case for our battle fleet. In particular, we will enhance our spaceships with the ability to launch missiles. We also want to support the requirement that launching a weapon requires that the spaceship is in a healthy state.
We can define a fire function that takes a launch function and a spaceship. The launch function is executed only when the spaceship is healthy. Why do we want to take a function as an argument? Because we want ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access