Guest User

Untitled

a guest
Dec 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. trait Monad<T> {
  2. fn ret(T) -> self;
  3. fn bind<U>(fn(&T) -> Monad<U>) -> Monad<U>;
  4. }
  5.  
  6. impl<T> Option<T>: Monad<T> {
  7. fn ret(value: T) -> Option<T> { Some(value) }
  8. fn bind<U>(cb: fn(&T) -> Option<U>) -> Option<U> {
  9. match self {
  10. Some(value) => cb(&value),
  11. None => None
  12. }
  13. }
  14. }
  15.  
  16. /*
  17. monad.rs:8:1: 13:2 error: method `bind` has an incompatible type: expected trait Monad but found enum core::option::Option
  18. monad.rs:8 fn bind<U>(cb: fn(&T) -> Option<U>) -> Option<U> {
  19. monad.rs:9 match self {
  20. monad.rs:10 Some(value) => cb(&value),
  21. monad.rs:11 None => None
  22. monad.rs:12 }
  23. monad.rs:13 }
  24. */
Add Comment
Please, Sign In to add comment