Monad Pattern

As with Monoids, Monads are a recurring theme in the Scala and Functional Programming world. This chapter presents a design pattern based on the concept of a Monad. In pure functional programming a monad is a structure that represents computations on a se

  • PDF / 252,831 Bytes
  • 6 Pages / 439.37 x 666.14 pts Page_size
  • 82 Downloads / 230 Views

DOWNLOAD

REPORT


Monad Pattern

39.1

Introduction

As with Monoids, Monads are a recurring theme in the Scala and Functional Programming world. This chapter presents a design pattern based on the concept of a Monad. In pure functional programming a monad is a structure that represents computations on a set of objects. The Monad Pattern takes the concepts behind the monad structure and defines how these can be used to create a container type that handles the creation and combination of Monads, the application of functions to members of that container, how pipelines of functions may be applied to the contents of the container and how multiple containers can be flattened into a single container. The Monad Pattern identifies a container of type T that provides a set of operations that can be grouped together as follows: Functor like operations • Possesses a unique identity value to which applying the functions in the Monad has no affect • The ability to apply a function to every object in the container (via a function fmap) returning a new container of the same type. • Functional combination applicability. That is combining two functions together and applying the combined function has the same effect as applying one function followed by a second function. Applicative Functor like operations • The pure operation. This operation places objects in a context (or container). • The apply operation. This operation takes one or more functions, already in a context (such as a List) and applies them to the objects already in the context. The result that is generated is also in the context.

J. Hunt, Scala Design Patterns: Patterns for Practical Reuse and Design, DOI 10.1007/978-3-319-02192-8_39, © Springer International Publishing Switzerland 2013

301

302

39

Monad Pattern

Monoid like operations • A combination (append) operation that combines the elements of the set together. With the additional of • An operation that takes multiple sets of values and combined them into a single set of values, often referred to as join. From this description you can see that a Monad is actually the combination of the Monoid pattern with the Applicative Functor (and thus Functor) patterns. Or to put it another way a Monad is an Applicative Functor and it is a Monoid (combined together). The one addition made by the Monad Pattern itself is the addition of a single operation join. Many of the collection classes in the Scala language are examples of the Monad Pattern.

39.2

Pattern Classification

Functional

39.3

Intent

To allow for the creation of object containers with a default set of operations that allow for the composition of the containers and the application of functions to the contents of the containers.

39.4

Context

Containers (or more generically contexts) for a group of objects that is generally useful. Standardising the way this is done brings major benefits in comprehension, (human) reusability and consistency.

39.5

Forces/Motivation

Many data structures represent common behaviour that when understood allow a new data structure to be q