Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create type classes for suspension functors #13

Open
anatoliykmetyuk opened this issue Dec 29, 2016 · 0 comments
Open

Create type classes for suspension functors #13

anatoliykmetyuk opened this issue Dec 29, 2016 · 0 comments

Comments

@anatoliykmetyuk
Copy link
Owner

Currently, a pattern emerges whenever we try to define all the type classes needed for the suspension functor:

trait EvalImpl {
  implicit val suspendedEval: Suspended[Eval] = new ( (() => ?) ~> Eval ) {
    override def apply[A](x: () => A): Eval[A] = Eval.always(x())
  }

  implicit val monoidKEval: MonoidK[Eval] = new MonoidK[Eval] {
    override def combineK[A](e1: Eval[A], e2: Eval[A]): Eval[A] = e2
    override def empty[A] = Eval.always[Nothing] { throw new NotImplementedError }
  }
}

trait FutureImpl {
  implicit val suspendedFuture: Suspended[Future] = new ( (() => ?) ~> Future ) {
    override def apply[A](x: () => A): Future[A] = Future { x() }
  }

  implicit val monoidKFuture: MonoidK[Future] = new MonoidK[Future] {
    override def combineK[A](e1: Future[A], e2: Future[A]): Future[A] = Future.firstCompletedOf(List(e1, e2))
    override def empty[A] = Future.never
  }

  implicit val comonadFuture: Comonad[Future] = new Comonad[Future] {
    override def extract[A](x: Future[A]): A = Await.result(x, Duration.Inf)
    override def coflatMap[A, B](fa: Future[A])(f: Future[A] => B): Future[B] = map(coflatten(fa))(f)
    override def map[A, B](fa: Future[A])(f: A => B): Future[B] = fa.map(f)
  }
}

Probably, these traits could be turned into type classes for arbitrary S[_]. Benefits from it:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant