k33g / stools

šŸ¼ https://k33g.github.io/stools/
https://k33g.github.io/stools/
MIT License
5 stars 0 forks source link

A thought about functors and monads #2

Open mmoqui opened 8 years ago

mmoqui commented 8 years ago

In software engineering, a functor is more than a simple box or container. It is a function that dresses our objects with a computational context and that gives the possibility to apply our usual morphisms (functions, methods, whatever the term used) onto such dressed objects. Maybe and Either are two famous functors. Maybe carries the context that an object can be undefined when applying a computation. Either carries the context that only one object among two possibilities exists (or is valid) when applying a computation.

One thing: a functor F must satisfy the two following properties:

With functors, we can also apply them on morphism to get a dressed morphism that can be applied later on dressed objects. Those are applicative functors. The applicative functors are then defined by an additional function like this:

Maybe is an applicative functor.

Monads are an extension of applicative functors. Their interest is to express sequential computation. So we need an additional function in order to apply a morphism onto a dressed object to get another dressed object from an undressed object. This is the bind function:

With bind, we can chain monad together to form a pipeline of computation. Maybe is a monad.

For the Kleisli triple I talk a bit in your talk: this is a formalism of three properties that express a monad. In short:

k33g commented 8 years ago

Hey šŸ‘‹ @mmoqui thank you for the comment, I will use it for the next version :)