Skip to content

Instantly share code, notes, and snippets.

@guizmaii
Forked from kevinmeredith/x.scala
Created May 28, 2018 16:24
Show Gist options
  • Save guizmaii/6b95bd40edb3e8d59c6ca8be08a7ec78 to your computer and use it in GitHub Desktop.
Save guizmaii/6b95bd40edb3e8d59c6ca8be08a7ec78 to your computer and use it in GitHub Desktop.
Run IO[List[Int]] in Parallel using Ammonite
@ import $ivy.`org.typelevel::cats-effect:0.10.1`
@ import cats._, cats.data._, cats.implicits._, cats.effect._
@ def lift(i: Int): IO[Int] = Timer[IO].sleep(1.second) >> IO {
println(Thread.currentThread.getName + "At time = " + java.time.Instant.now)
i
}
@ (0 to 10).toList.map(i => Concurrent[IO].start(lift(i)))
res41: List[IO[Fiber[IO, Int]]] = List(
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@2faeea2),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@3e8c8be5),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@108a2dcd),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@d21918f),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@58fbe177),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@2729a2e),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@43d73be8),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@427b77f4),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@450cd695),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@533987f0),
Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@29cbfe04)
)
@ Traverse[List].sequence(res41)
res42: IO[List[Fiber[IO, Int]]] = Map(
Bind(Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@2faeea2), cats.FlatMap$$Lambda$2914/1434934951@60e127c1),
scala.Function2$$Lambda$930/320860012@7d3aaf87,
0
)
@ res42.map(_.map(_.join))
res43: IO[List[IO[Int]]] = Map(
Bind(Async(cats.effect.internals.IOStart$$$Lambda$2681/256186736@2faeea2), cats.FlatMap$$Lambda$2914/1434934951@60e127c1),
scala.Function1$$Lambda$36/152134087@685766df,
1
)
@ res43.map(_.sequence).flatten.unsafeRunSync
scala-execution-context-global-2441At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2439At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2439At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2657At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2439At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2657At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2439At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2660At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2657At time = 2018-05-28T16:15:58.495Z
scala-execution-context-global-2438At time = 2018-05-28T16:15:58.496Z
scala-execution-context-global-2441At time = 2018-05-28T16:15:58.496Z
res44: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment