This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Function type being | |
| (A) ⇒ T | |
| //we apply | |
| (T) ⇒ U | |
| //the returned type will be | |
| (A) ⇒ U | |
| def map[T, U](source: (A) ⇒ T)(f: (T) ⇒ U): (A)⇒ U |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def map[T, P >: T, U](source: (A) ⇒ T)(f: (P) ⇒ U): (A)⇒ U = f.compose(source) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def map[T, P >: T, U](source: (A) ⇒ T)(f: (P) ⇒ U): (A)⇒ U = (x: A) ⇒ f(source(x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def apply[T](data: T): (A) ⇒ T = ( _ ⇒ data) | |
| def flatten[T](m: (A) ⇒ (A) ⇒ T): (A) ⇒ T = (x: A) ⇒ m(x)(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case class FunctionApplicative[A]() extends Applicative[({type λ[α] = Function[A,α]})#λ]{ | |
| def apply[T](data: T): (A) ⇒ T = ( _ ⇒ data) | |
| def flatten[T](m: (A) ⇒ (A) ⇒ T): (A) ⇒ T = (x: A) ⇒ m(x)(x) | |
| def map[T, P >: T, U](source: (A) ⇒ T)(f: (P) ⇒ U): (A)⇒ U = f.compose(source) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def `*2` = (x: Int) ⇒ x * 2 | |
| def `+10` = (x: Int) ⇒ 10 + x | |
| println((for { | |
| a ← `*2` | |
| b ← `+10` | |
| } yield (a + b))(3)) | |
| //produces 19 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case class Config[T](data: T) | |
| implicit object ApplicativeConfig extends Applicative[Config] { | |
| def apply[T](data: T) = Config(data) | |
| def flatten[T](m: Config[Config[T]]) = m.data | |
| def map[T, P >: T, U](source: Config[T])(f: (P) => U) = Config(f(source.data)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sequence[T, A[_]](input: List[A[T]])(implicit applicative: Applicative[A]): A[List[T]] = { | |
| import applicative._ | |
| input match { | |
| case (x :: xs) ⇒ | |
| def cons(head: T, list: List[T]): List[T] = head :: list | |
| liftA2(cons, x, sequence(xs)) | |
| case _ ⇒ pure(List.empty[T]) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Traverse[C[_]] { | |
| def traverse[M[_], T, U](source: C[T])(f: (T) ⇒ M[U])(implicit applicative: Applicative[M]): M[C[U]] | |
| final def sequence[M[_], T](source: C[M[T]])(implicit applicative: Applicative[M]): M[C[T]] = { | |
| traverse[M, M[T], T](source){identity} | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait ListLike[C[_]] { | |
| def cons[T](x: T, xs: C[T]): C[T] | |
| def empty[T](): C[T] | |
| def first[T](source: C[T]): Option[T] | |
| def rest [T](source: C[T]): C[T] | |
| } |