Pattern 20Customized Control Flow

Intent

To create focused, custom-control flow abstractions

Overview

Using the right control flow abstraction for the job can help us write clearer code. For instance, Ruby includes an unless operator, which can be used to do something unless a conditional is true. Good Ruby code uses this operator over if and the not operator, since it’s clearer to read.

No language has every useful control flow abstraction built in, though. Functional programming languages give us a way to create our own using higher-order functions. For instance, to create a control flow structure that executes a piece of code n times and prints out the average time for the runs, we can write a function that takes another function and invokes ...

Get Functional Programming Patterns in Scala and Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.