Created
April 14, 2024 13:45
-
-
Save sshark/a46a7281a25c852ceb7828bf4d21c3c2 to your computer and use it in GitHub Desktop.
This file contains 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
//import cats.Applicative | |
//import cats.implicits.* | |
/* | |
def sequence[A, F[_], G[_]](fga: F[G[A]])(using Traverse[F], Applicative[G]): G[F[A]] = | |
traverse(fga)(identity) | |
trait Traverse[F[_]]: | |
def traverse[A, B, G[_]](fa: F[A])(f: A => G[B])(using Applicative[G]): G[F[B]] | |
*/ | |
trait Functor[F[_]] { | |
def map[A, B](fa: F[A])(f: A => B): F[B] | |
} | |
trait Applicative[F[_]] extends Functor[F] { | |
override def map[A, B](fa: F[A])(f: A => B): F[B] = ??? | |
def apply[A, B](fa: F[A])(ff: F[A => B]): F[B] = ??? | |
def map2[A, B, C](fa: F[A])(fb: F[B])(f: (A, B) => C)(implicit ev: Functor[F]): F[C] = | |
apply(fb)(ev.map(fa)(a => (b => f(a, b)))) | |
} | |
/* | |
val foo: Either[Nothing, List[Int]] = List(Right(1), Right(2)).traverse(r => r.asInstanceOf[Either[Nothing, Int]]) | |
println(foo) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment