Last active
March 24, 2017 15:36
-
-
Save xuwei-k/ff8455a7f8c77dbacee0ba9394501e30 to your computer and use it in GitHub Desktop.
port https://github.com/purescript/purescript-parallel to 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
libraryDependencies ++= Seq( | |
"org.scalaz" %% "scalaz-concurrent" % "7.2.10" | |
) | |
addCompilerPlugin("org.spire-math" % "kind-projector" % "0.9.3" cross CrossVersion.binary) | |
scalaVersion := "2.12.1" |
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 scalaz._ | |
import scalaz.concurrent.Task | |
import scalaz.concurrent.Task._ | |
object Main { | |
def main(args: Array[String]): Unit = { | |
taskSample() | |
taskReaderSample() | |
} | |
def taskReaderSample(): Unit = { | |
val x = Parallel.parTraverse[ | |
Kleisli[Task, String, ?], | |
IList, | |
Int, | |
Int | |
](IList(1, 2, 3, 4)){ | |
a => Kleisli[Task, String, Int](s => | |
Task{ | |
println(s"start $s $a") | |
Thread.sleep(1000) | |
println(s"end $s $a") | |
a | |
} | |
) | |
} | |
println(x("foo").unsafePerformSync) | |
} | |
def taskSample(): Unit = { | |
val x: Task[IList[Int]] = Parallel.parTraverse(IList(1, 2, 3, 4)){ | |
a => Task{ | |
println(s"start $a") | |
Thread.sleep(1000) | |
println(s"end $a") | |
a | |
} | |
} | |
println(x.unsafePerformSync) | |
} | |
} |
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
package scalaz | |
import scalaz.concurrent.Task | |
import scalaz.concurrent.Task._ | |
abstract class Parallel[M[_]](implicit val M: Monad[M]) { | |
type F[_] | |
implicit def F: Applicative[F] | |
def parallel: M ~> F | |
def sequential: F ~> M | |
} | |
object Parallel { | |
def apply[M[_]](implicit M: Parallel[M]): Parallel[M] = M | |
def parTraverse[M[_], T[_], A, B](ta: T[A])(f: A => M[B])(implicit | |
P: Parallel[M], | |
T: Traverse[T] | |
): M[T[B]] = { | |
P.sequential(T.traverse[P.F, A, B](ta)(a => P.parallel(f(a)))(P.F)) | |
} | |
implicit val task: Parallel[Task] = | |
new Parallel[Task] { | |
override type F[A] = ParallelTask[A] | |
override def F = Applicative[ParallelTask] | |
override val parallel = | |
Lambda[Task ~> ParallelTask](Tags.Parallel(_)) | |
override val sequential = | |
Lambda[ParallelTask ~> Task](Tags.Parallel.unwrap(_)) | |
} | |
implicit def readerT[M[_], E](implicit P: Parallel[M]): Parallel[ReaderT[M, E, ?]] = { | |
import P._ | |
new Parallel[ReaderT[M, E, ?]] { | |
override type F[A] = ReaderT[P.F, E, A] | |
override def F = ReaderT.kleisliApplicative[P.F, E](P.F) | |
override val parallel = | |
Lambda[ReaderT[M, E, ?] ~> ReaderT[P.F, E, ?]](_.transform(P.parallel)) | |
override val sequential = | |
Lambda[ReaderT[P.F, E, ?] ~> ReaderT[M, E, ?]](_.transform(P.sequential)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment