Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. pub trait Unplug {
  2. type Gen;
  3. type A;
  4. }
  5.  
  6. pub trait Plug<A> {
  7. type Out: Unplug<A = A>;
  8. }
  9.  
  10. pub trait Functor: Unplug + Plug<<Self as Unplug>::A> {
  11. fn map<B, F>(self, f: F) -> <Self as Plug<B>>::Out
  12. where
  13. Self: Plug<B>,
  14. F: FnMut(<Self as Unplug>::A) -> B;
  15. }
  16.  
  17. pub trait Applicative: Functor {
  18. fn pure(s: <Self as Unplug>::A) -> Self;
  19. fn app<B, F>(self, f: <Self as Plug<F>>::Out) -> <Self as Plug<B>>::Out
  20. where
  21. F: FnOnce(<Self as Unplug>::A) -> B,
  22. Self: Plug<F> + Plug<B>;
  23. }
  24.  
  25. pub trait Monad: Applicative {
  26. fn bind<F, B>(self, f: F) -> <Self as Plug<B>>::Out
  27. where
  28. Self: Plug<F> + Plug<B>,
  29. F: FnMut(<Self as Unplug>::A) -> <Self as Plug<B>>::Out;
  30. }
  31.  
  32. impl<A> Unplug for Option<A> {
  33. type Gen = Option<A>;
  34. type A = A;
  35. }
  36.  
  37. impl<A, B> Plug<B> for Option<A> {
  38. type Out = Option<B>;
  39. }
  40.  
  41. impl<A> Functor for Option<A> {
  42. fn map<B, F>(self, f: F) -> <Self as Plug<B>>::Out
  43. where
  44. F: FnMut(<Self as Unplug>::A) -> B,
  45. {
  46. self.map(f)
  47. }
  48. }
  49.  
  50. impl<A> Applicative for Option<A> {
  51. fn pure(a: A) -> Self {
  52. Some(a)
  53. }
  54.  
  55. fn app<B, F>(self, fs: <Self as Plug<F>>::Out) -> <Self as Plug<B>>::Out
  56. where
  57. F: FnOnce(<Self as Unplug>::A) -> B,
  58. {
  59. self.map(fs?)
  60. }
  61. }
  62.  
  63. impl<A: Clone> Monad for Option<A> {
  64. fn bind<F, B>(self, f: F) -> <Self as Plug<B>>::Out
  65. where
  66. F: FnMut(<Self as Unplug>::A) -> <Self as Plug<B>>::Out,
  67. {
  68. self.and_then(f)
  69. }
  70. }
  71.  
  72. impl<A> Unplug for Vec<A> {
  73. type Gen = Vec<A>;
  74. type A = A;
  75. }
  76.  
  77. impl<A, B> Plug<B> for Vec<A> {
  78. type Out = Vec<B>;
  79. }
  80.  
  81. impl<A> Functor for Vec<A> {
  82. fn map<B, F>(self, f: F) -> <Self as Plug<B>>::Out
  83. where
  84. F: FnMut(<Self as Unplug>::A) -> B,
  85. {
  86. self.into_iter().map(f).collect()
  87. }
  88. }
  89.  
  90. impl<A: Clone> Applicative for Vec<A> {
  91. fn pure(a: A) -> Self {
  92. vec![a]
  93. }
  94.  
  95. fn app<B, F>(self, fs: <Self as Plug<F>>::Out) -> <Self as Plug<B>>::Out
  96. where
  97. F: FnOnce(<Self as Unplug>::A) -> B,
  98. {
  99. self.into_iter().zip(fs).map(|(x, f)| f(x)).collect()
  100. }
  101. }
  102.  
  103. impl<A: Clone> Monad for Vec<A> {
  104. fn bind<F, B>(self, f: F) -> <Self as Plug<B>>::Out
  105. where
  106. F: FnMut(<Self as Unplug>::A) -> <Self as Plug<B>>::Out,
  107. {
  108. self.into_iter().flat_map(f).collect()
  109. }
  110. }
  111.  
  112. pub fn functor_test<F: Functor, A, B, C>(
  113. functor: F,
  114. fun: impl Fn(A) -> B,
  115. fun2: impl Fn(B) -> C,
  116. ) -> <F as Plug<C>>::Out
  117. where
  118. F: Plug<A> + Plug<B> + Plug<C> + Unplug<A = A>,
  119. {
  120. functor.map(|x| fun2(fun(x)))
  121. }
  122.  
  123. #[test]
  124. fn test_f() {
  125. let x = Functor::map(Some(3), |x| x + 1);
  126.  
  127. assert_eq!(x, Some(4));
  128.  
  129. let x = None::<i32>.map(|x| x + 1);
  130.  
  131. assert_eq!(x, None);
  132.  
  133. let x = functor_test(Some(3), |x| x + 1, |x| x * 2);
  134.  
  135. assert_eq!(x, Some(8));
  136.  
  137. let x = functor_test(None::<i32>, |x| x + 1, |x| x * 2);
  138.  
  139. assert_eq!(x, None);
  140.  
  141. assert_eq!(None::<i32>.bind(|x| x.checked_add(1)), None);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement