Skip to contents

Classes implementing methods for these S7 generics are called monads. fmap() should be implemented such that the functor laws hold. bind() or join() should be implemented such that the monad laws hold. %>>% is the fmap() pipe operator, and %>-% is the bind() pipe operator. Operator usage is in the form m %>>% f(...).

Usage

lhs %>>% rhs

lhs %>-% rhs

fmap(m, f, ...)

bind(m, f, ...)

join(m)

Arguments

m, lhs

A monadic object.

f, rhs

A function. For bind(), it should return a monadic object.

...

Additional arguments passed to f.

Details

Monads are containers for values. fmap() transforms the contained value with a function. bind() transforms the contained value with a function that returns a monadic object. join() takes a monad whose contained value is another monad, and combines them into a new monadic object. It is used to unwrap a layer of monadic structure. Implementing classes typically embed some form of control flow or state management in bind() or join().

There is a default implementation for join() if you provide bind(), and a default implementation for bind() if you provide join() and fmap(). For performance reasons you may wish to implement both regardless.

Operators

The pipe operators expect a monadic object as lhs and a call expression or a function as rhs. The pipe expression is transformed into a call to the corresponding monad generic with arguments to the call in rhs passed as additional arguments ... to f in the generic. For example, m %>>% f(x) is equivalent to fmap(m, f, x) and m %>-% f(x) is equivalent to bind(m, f, x).

Trivia

A class that only implements fmap() is called a functor.

See also

The monad laws and functor laws that implementations should satisfy.