SHOW:
|
|
- or go back to the newest paste.
1 | package extension.monad | |
2 | ||
3 | trait Monad[A, M[_]] { | |
4 | - | // >>= :: Monad m => m a -> (a -> m b) -> m b |
4 | + | // >>= :: Monad m => m a -> (a -> m b) -> m b |
5 | - | def flatMap[B](input: A => M[B]): M[B] // AKA "bind" |
5 | + | def flatMap[B](input: A => M[B]): M[B] // AKA "bind" |
6 | } | |
7 | ||
8 | trait Functor[A, F[_]] { | |
9 | - | // fmap :: Functor f => (a -> b) -> f a -> f b |
9 | + | // fmap :: Functor f => (a -> b) -> f a -> f b |
10 | - | def map[B](input: A => B): F[B] // AKA "fmap" |
10 | + | def map[B](input: A => B): F[B] // AKA "fmap" |
11 | } | |
12 | ||
13 | object MOption { | |
14 | implicit class MonadicOption[A](left: Option[A]) extends Monad[A, Option] with Functor[A, Option] { | |
15 | def flatMap[B](right: A => Option[B]): Option[B] = left match { | |
16 | case Some(x) => right(x) | |
17 | case None => None | |
18 | } | |
19 | ||
20 | def map[B](right: A => B): Option[B] = left match { | |
21 | case Some(x) => Some(right(x)) | |
22 | case None => None | |
23 | } | |
24 | } | |
25 | } |