I. Semigroup | |
---|---|
Examples Constructor: * |
Set of whole numbers with + or * operation: (4 + 10) + 2 = 4 + (10 +2) Examples without identity: positive whole numbers with + operator, or not-empty sequences.
|
Laws |
Closure and Associativity |
Operation |
append |
II. Monoid | |
---|---|
Examples |
Set of whole numbers with + or * operation: a+0=a, a*1=a Lists, Bool, Ordering, Maybe, selfdefined Tree. Collection with operation, which you (recursively) can expand.
|
Laws |
Like Semigroup, with Identity
mconcat is a secundairy function that reduces a list of monoids by a fold-operation to a single value. (mconcat is an unfortunate name, in an addition for instance nothing is joined together.) |
Operations |
Like Semigroep, with the zero-operation added. |
III. Functor | |
---|---|
Examples Unary type constructor: *->* Binary type constructor: *->*->* Functor[F[_]] |
Scala: List, Option, Set, Either, Try, Future... Haskell: List, Maybe, eigen Tree, Either, Function ... "Everything that can be mapped over", applying a function to one or more values in a context.
Mapping over a function is function-composition (Nicholas, see also LYAH Functor)
|
Laws |
Identity and Compose
|
Operation |
One typeclass-method: fmap :: (a -> b) -> f a -> f b (map is the List-implementation of fmap) Map, Either, Function1 are binary, for which the operation must be applied partially, in steps.
|
IV. Applicative (-Functor) | |
---|---|
Examples |
The applied function itself can also be wrapped in a context |
Laws |
|
Operations |
Like Functor, with Pure added (Wraps a value in an applicative), and ap or <*>(Takes an applicative containing a function and an other applicative, extracts the function(s) from the first and maps it over the second). Applicatives are composable, which is not the case for every Monad. |
V. Monad | |
---|---|
Examples Higher order type operator: (*->*)->* Monad[M[_]] M[_] is an unary type-constructor like List or Option
|
Scala: List, Option, Set, Either, Try, Future... Haskell: List, Maybe, Tree?, Either, ... "Informaly a Monad is everything with a constructor and a flatMap (or bind) method."
|
Laws |
|
Operations
|
Like Applicative, with bind, >>= or do-notation. (In Scala: flatMap.) Next to bind, return takes care of the wrapping of a value in the monad (a "lift"-function, like pure of the applicative), and >> ("then") takes care of the chaining of monadic actions that don't need the result of the predecessor. fail is just needed internally for pattern-match failures in do-notation. |