Skip to content

Instantly share code, notes, and snippets.

@oakwhiz
Created May 4, 2014 09:00
Show Gist options
  • Save oakwhiz/7ae602aa069ef421371f to your computer and use it in GitHub Desktop.
Save oakwhiz/7ae602aa069ef421371f to your computer and use it in GitHub Desktop.
template <class <typename> F>
struct Functor : private Applicative<F> {
template <typename T, typename Function>
static F<T> fmap(Function&& f, F<T> x) {
return ap(pure(move(f)), move(x));
}
};
template <class <typename> M>
struct Applicative<M> : private Monad<M> {
template <typename T>
static M<T> pure(T&& x) {
return ret(forward<T>(x));
}
template <typename Function, typename T>
static M<T> ap(M<Function> mf, M<T> mx) {
return bind(move(x), [&mx](Function f) {
return bind(
move(mx),
[&f](T x) {
return ret(f(move(x)));
}
);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment