Skip to content

Commit

Permalink
Little typeclass to run two IOs in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
ktonga committed May 15, 2018
1 parent 40c06f6 commit 824c234
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Dependencies {

lazy val compile = Seq(
dep("org.scalaz", "7.2.21", "scalaz-core"),
dep("org.scalaz", "2.1.0", "scalaz-ioeffect", "scalaz-ioeffect-cats"),
dep("org.scalaz", "2.2.0", "scalaz-ioeffect", "scalaz-ioeffect-cats"),
dep("com.codecommit", "1.2.1", "shims"),
dep("co.fs2", "0.10.3", "fs2-core"),
dep("eu.timepit", "0.8.7", "refined-scalaz"),
Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/com/brickx/Par.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.brickx

import std._

trait Par[F[_]] {
def apply2[A, B, C](fa: F[A], fb: F[B])(ff: (A, B) => C): F[C]
}

object Par {
@inline def apply[F[_]](implicit F: Par[F]): Par[F] = F

implicit def ioPar[E]: Par[IO[E, ?]] = new Par[IO[E, ?]] {
override def apply2[A, B, C](fa: IO[E, A], fb: IO[E, B])(ff: (A, B) => C): IO[E, C] =
fa.par(fb).map(ff.tupled)
}
}
6 changes: 3 additions & 3 deletions src/main/scala/com/brickx/autoinvest/Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ object Program {

type ComputeAutoInvest = TradingData => Result[CreateOrderRequest]

def default[F[_]: Sync](events: Events[F],
def default[F[_]: Sync: Par](events: Events[F],
trading: Trading[F],
db: Db[F],
computeAutoInvest: ComputeAutoInvest)(implicit clock: Clock): Program[F] =
new DefaultProgram(events, trading, db, computeAutoInvest)

class DefaultProgram[F[_]: Sync](events: Events[F],
class DefaultProgram[F[_]: Sync: Par](events: Events[F],
trading: Trading[F],
db: Db[F],
computeAutoInvest: ComputeAutoInvest)(implicit clock: Clock)
Expand All @@ -48,7 +48,7 @@ object Program {
}

private def fetchTradingData(accountId: AccountId): F[(AccountId, Result[TradingData])] =
^(
Par[F].apply2(
trading.getPortfolio(accountId, "today".just),
trading.getOrderBook("some-prop")
)(TradingData.apply).attemptR.map((accountId, _))
Expand Down
1 change: 1 addition & 0 deletions src/test/scala/com/brickx/autoinvest/ProgramSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,5 @@ trait Fakes {
override def savePending(pending: Pending): Task[Unit] =
IO.point(pendings = pendings :+ pending).toUnit
}

}

0 comments on commit 824c234

Please sign in to comment.