*** Welcome to piglix ***

Monad (functional programming)


In functional programming, monads are a way to build computer programs by joining simple components in robust ways. A monad may encapsulate values of a particular data type, creating a new type associated with a specific computation. For example, the simple Maybe monad is a way to represent option types, encapsulating a type of variables which may have a null value, and ensuring that null values are not passed as arguments to functions that cannot handle them. The monad represents computations with a sequential structure: a monad defines what it means to chain operations together. This allows the programmer to build pipelines that process data in a series of steps (i.e. a series of actions applied to the data), in which each action is decorated with the additional processing rules provided by the monad. A monad is defined by a return operator that creates values, and a bind operator used to link the actions in the pipeline; this definition must follow a set of axioms called monad laws, which are needed for composition to work properly.

Monads allow a programming style where programs are written by putting together highly composable parts, combining in flexible ways the possible actions that can work on a particular type of data. As such, monads have been described as "programmable semicolons"; a semicolon is the operator used to chain together individual statements in many imperative programming languages, thus the expression implies that extra code will be executed between the actions in the pipeline. Monads have also been explained with a physical metaphor as assembly lines, where a conveyor belt transports data between functional units that transform it one step at a time. They can also be seen as a functional design pattern to build generic types.


...
Wikipedia

...