FP - Relationship between Applicative Functor and Monad

less than 1 minute read

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.

Example)

1
2
3
4
5
6
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)

1
2
3
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)

1
2
3
4
5
6
Optional<int> join(Optional<Optional<int>> v) {
   if (v) {
      return *v;
   }
   return Optional<int>(nullptr);
}

Monad C++ definition:

1
2
3
4
template<typename T, typename U>
Monad<U> bind(Monad<T> m, std::function<Monad<U>(T)> f) {
   return join(apply(pure(f), m));
}