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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.