Pattern 16Function Builder

Intent

To create a function that itself creates functions, allowing us to synthesize behaviors on the fly

Overview

Sometimes we’ve got a function that performs a useful action, and we need a function that performs some other, related action. We might have a vowel? predicate that returns true when a vowel is passed in and need a consonant? that does the same for consonants.

Other times, we’ve got some data that we need to turn into an action. We might have a discount percentage and need a function that can apply that discount to a set of items.

With Function Builder, we write a function that takes our data or function (though, as we’ve seen, the distinction between functions and data is blurry) and uses it to create ...

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.