Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<list>
- #include<optional>
- template<template <typename> class F>
- struct Functor {
- template<typename A, typename B>
- static F<B> fmap(B *(f)(A), F<A> val);
- // ($>)
- template<typename A, typename B>
- static F<A> replaceFunctor(A a, F<B> list);
- };
- template<>
- struct Functor<std::list> {
- template<typename A, typename B>
- static std::list<B> fmap(B *(f)(A), std::list<A> list) {
- for (auto x : list) x = f(x);
- }
- template<typename A, typename B>
- static std::list<A> replaceFunctor(A a, std::list<B> list) {
- return new std::list<A>(a);
- }
- };
- template<>
- struct Functor<std::optional> {
- template<typename A, typename B>
- static std::optional<B> fmap(B *(f)(A), std::optional<A> maybe) {
- return maybe.has_value() ? maybe.emplace(f(maybe.get())) : maybe;
- }
- template<typename A, typename B>
- static std::optional<A> replaceFunctor(A a, std::optional<B> maybe) {
- return new std::optional(a);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement