4.2 Programming with Closures
We’re talking about the power and elegance of closures in this chapter, but first let’s discuss how to approach them in our projects. We need to decide whether we want to implement a certain functionality or task as a regular function/method or whether we should use a closure.
Closures augment, refine, or enhance another piece of code. For example, a closure may be useful to express a predicate or condition that will refine the selection of objects. We can use closures to take advantage of coroutines such as the control flow like in iterators or loops.
Closures are very helpful in two specific areas. They can help manage resource cleanup (see Section 4.5, Using Closures for Resource Cleanup) and they can help ...
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