-
-
Save ShahOdin/78e17e4f011299a6f7ed6184e394330e to your computer and use it in GitHub Desktop.
Par
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 scala.concurrent.duration.Duration | |
import scala.concurrent.{Await, ExecutionContext, Future} | |
object ParImpl { | |
sealed trait Par[A] { | |
def toFuture(implicit ec: ExecutionContext): Future[A] = { | |
this match { | |
case Par.Unit(f) => Future(f()) | |
case x:Par.Map2[A,_,_] => | |
for { | |
aVal <- x.parLeft.toFuture | |
bVal <- x.parRight.toFuture | |
} yield x.map(aVal, bVal) | |
} | |
} | |
def getSequentially: A = this match { | |
case Par.Unit(f) => f() | |
case x:Par.Map2[A,_,_] => | |
x.map(x.parRight.getSequentially, x.parLeft.getSequentially) | |
} | |
def get(implicit ec: ExecutionContext): A = Await.result(toFuture, Duration.Inf) | |
} | |
object Par { | |
case class Unit[A](v: () => A) extends Par[A] | |
case class Map2[A, B, C](parLeft: Par[B], | |
parRight: Par[C], | |
map: (B, C) => A) extends Par[A] | |
def unit[A](a: => A): Par[A] = Par.Unit(() => a) | |
def map2[A, B, C](a: Par[B], b: Par[C])(f: (B, C) => A): Par[A] = Par.Map2(a, b, f) | |
} | |
} | |
object Doodle extends App { | |
import ParImpl._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
def sum(ints: IndexedSeq[Int]): Par[Int] = | |
if (ints.length <= 1) | |
Par.unit(ints.headOption.getOrElse(0)) | |
else { | |
val (l, r) = ints.splitAt(ints.length / 2) | |
Par.map2(sum(l), sum(r))(_ + _) | |
} | |
val summedNumbers = sum(IndexedSeq(1, 2, 3, 4)).get | |
println(summedNumbers) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment