Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include<list>
  2. #include<optional>
  3.  
  4. template<template <typename> class F>
  5. struct Functor {
  6. template<typename A, typename B>
  7. static F<B> fmap(B *(f)(A), F<A> val);
  8.  
  9. // ($>)
  10. template<typename A, typename B>
  11. static F<A> replaceFunctor(A a, F<B> list);
  12. };
  13.  
  14. template<>
  15. struct Functor<std::list> {
  16. template<typename A, typename B>
  17. static std::list<B> fmap(B *(f)(A), std::list<A> list) {
  18. for (auto x : list) x = f(x);
  19. }
  20.  
  21. template<typename A, typename B>
  22. static std::list<A> replaceFunctor(A a, std::list<B> list) {
  23. return new std::list<A>(a);
  24. }
  25. };
  26.  
  27. template<>
  28. struct Functor<std::optional> {
  29. template<typename A, typename B>
  30. static std::optional<B> fmap(B *(f)(A), std::optional<A> maybe) {
  31. return maybe.has_value() ? maybe.emplace(f(maybe.get())) : maybe;
  32. }
  33.  
  34. template<typename A, typename B>
  35. static std::optional<A> replaceFunctor(A a, std::optional<B> maybe) {
  36. return new std::optional(a);
  37. }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement