-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factor ParallelViaProduct to possibly use elsewhere
- Loading branch information
Showing
2 changed files
with
51 additions
and
35 deletions.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
core/src/main/scala/org/bykn/bosatsu/ParallelViaProduct.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.bykn.bosatsu | ||
|
||
import cats.arrow.FunctionK | ||
import cats.{Parallel, Applicative} | ||
|
||
// Implement Parallel by implementing just Product | ||
abstract class ParallelViaProduct[G[_]] extends Parallel[G] { self => | ||
|
||
type F[A] = G[A] | ||
val parallel = new FunctionK[G, F] { | ||
def apply[A](ga: G[A]) = ga | ||
} | ||
|
||
val sequential = new FunctionK[F, G] { | ||
def apply[A](fa: F[A]) = fa | ||
} | ||
|
||
def parallelProduct[A, B](fa: F[A], fb: F[B]): F[(A, B)] | ||
|
||
val applicative: Applicative[F] = | ||
new Applicative[F] { self => | ||
def pure[A](a: A) = monad.pure(a) | ||
|
||
final def ap[A, B](ff: F[A => B])(fa: F[A]): F[B] = | ||
monad.map(parallelProduct(ff, fa)) { case (fn, a) => fn(a) } | ||
|
||
override def map[A, B](fa: F[A])(fn: A => B) = monad.map(fa)(fn) | ||
|
||
override def product[A, B](fa: F[A], fb: F[B]) = parallelProduct(fa, fb) | ||
} | ||
} |