Advertisement
Guest User

Untitled

a guest
May 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import cats.{Foldable, Monoid, Traverse}
  2. import cats.instances.int._
  3. import cats.instances.future._
  4. import cats.instances.vector._
  5.  
  6. import cats.syntax.foldable._
  7. import cats.syntax.semigroup._
  8. import cats.syntax.traverse._
  9.  
  10. import scala.concurrent._
  11. import scala.concurrent.duration._
  12. import scala.concurrent.ExecutionContext.Implicits.global
  13.  
  14. def foldMap[A, B: Monoid](vec: Vector[A])(f: A => B): B =
  15. vec.foldLeft(Monoid[B].empty)(_ |+| f(_))
  16.  
  17. def parallelFoldMap[A, B: Monoid]
  18. (values: Vector[A])
  19. (func: A => B): Future[B] = {
  20. val cpuNum = Runtime.getRuntime.availableProcessors
  21. val groupSize = (1.0 * values.size / cpuNum).ceil.toInt
  22.  
  23. /**
  24. * The call to vector.grouped returns an Iterable[Iterator[Int]]. We
  25. * sprinkle calls to toVector through the code to convert the data back
  26. * to a form that Cats can understand. The call to traverse creates a
  27. * Future[Vector[Int]] containing one Int per batch. The call to map then
  28. * combines the match using the combineAll method from Foldable.
  29. */
  30. values
  31. .grouped(groupSize)
  32. .toVector
  33. .traverse(group => Future(group.toVector.foldMap(func)))
  34. .map(_.combineAll)
  35. }
  36.  
  37. val future: Future[Int] =
  38. parallelFoldMap((1 to 1000).toVector)(_ * 1000)
  39.  
  40. Await.result(future, 1.second)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement