FP – Relationship between Applicative Functor and Monad

Applicative Functor has the following two properties.

Pure: create a functor from value.
Apply: a function taking arguments which are a Functor having a function and a Pure. This function will return the Functor after applying the function inside the first Functor with a value from a Pure by unwrapping.


Optional<int> apply(Optional<function<int(std::string)>> f, Optional<std::string> v) {
   if (f & v) {
      return Optional<int>(*f (*v));

   return Optional(nullptr);

Java example)

Optional<Integer> apply(Optional<Function<String, Integer>> f, Optional<String> v) {
   return f.flatMap(a -> v.map(a(v))

Monad is the Applicative Functor with joining to flat the returning Functor.
So Monad is Applicative Functor with a join.
Join: flattening the Functor

Join Example)

Optional<int> join(Optional<Optional<int>> v) {
   if (v) {
      return *v;

   return Optional<int>(nullptr);

Monad C++ definition:

template<typename T, typename U>
Monad<U> bind(Monad<T> m, std::function<Monad<U>(T)> f) {
   return join(apply(pure(f), m));
This entry was posted in Functional Programming, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s