Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.{Foldable, Monoid, Traverse}
- import cats.instances.int._
- import cats.instances.future._
- import cats.instances.vector._
- import cats.syntax.foldable._
- import cats.syntax.semigroup._
- import cats.syntax.traverse._
- import scala.concurrent._
- import scala.concurrent.duration._
- import scala.concurrent.ExecutionContext.Implicits.global
- def foldMap[A, B: Monoid](vec: Vector[A])(f: A => B): B =
- vec.foldLeft(Monoid[B].empty)(_ |+| f(_))
- def parallelFoldMap[A, B: Monoid]
- (values: Vector[A])
- (func: A => B): Future[B] = {
- val cpuNum = Runtime.getRuntime.availableProcessors
- val groupSize = (1.0 * values.size / cpuNum).ceil.toInt
- /**
- * The call to vector.grouped returns an Iterable[Iterator[Int]]. We
- * sprinkle calls to toVector through the code to convert the data back
- * to a form that Cats can understand. The call to traverse creates a
- * Future[Vector[Int]] containing one Int per batch. The call to map then
- * combines the match using the combineAll method from Foldable.
- */
- values
- .grouped(groupSize)
- .toVector
- .traverse(group => Future(group.toVector.foldMap(func)))
- .map(_.combineAll)
- }
- val future: Future[Int] =
- parallelFoldMap((1 to 1000).toVector)(_ * 1000)
- Await.result(future, 1.second)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement