Pattern 2Replacing State-Carrying Functional Interface

Intent

To encapsulate a bit of state along with program logic so it can be passed around, stored in data structures, and generally treated like any other first-class construct

Overview

In Pattern 1, Replacing Functional Interface, we saw how to replace Functional Interface with higher-order functions, but the instances we looked at didn’t carry around any program state. In this pattern, we’ll take a look at how we can replace Functional Interface implementations that need state using a powerful construct called a closure.

Also Known As

Function Object Functoid Functor

Functional Replacement

Functions in the functional world are part of a powerful construct called a closure. A closure wraps ...

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.