Guest User

Untitled

a guest
Oct 21st, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. object Fiddle {
  2. val f1 = Future {
  3. throw new Throwable("baaa") // emulating a future that bumped into an exception
  4. }
  5.  
  6. val f2 = Future {
  7. Thread.sleep(3000L) // emulating a future that takes a bit longer to complete
  8. 2
  9. }
  10.  
  11. val lf = List(f1, f2) // in the general case, this would be a dynamically sized list
  12.  
  13. val seq = Future.sequence(lf)
  14.  
  15. seq.onComplete {
  16. _ => lf.foreach(f => println(f.isCompleted))
  17. }
  18. }
  19.  
  20. val a = FuturesSequence
  21.  
  22. true
  23. false
  24.  
  25. import scala.util.{ Failure, Success, Try }
  26.  
  27. val lifted: List[Future[Try[Int]]] = List(f1, f2).map(
  28. _.map(Success(_)).recover { case t => Failure(t) }
  29. )
  30.  
  31. import scala.util.{ Failure, Success, Try }
  32.  
  33. private def lift[T](futures: Seq[Future[T]]) =
  34. futures.map(_.map { Success(_) }.recover { case t => Failure(t) })
  35.  
  36. def waitAll[T](futures: Seq[Future[T]]) =
  37. Future.sequence(lift(futures)) // having neutralized exception completions through the lifting, .sequence can now be used
  38.  
  39. waitAll(SeqOfFutures).map {
  40. // do whatever with the completed futures
  41. }
  42.  
  43. try {
  44. val a = Await.result(Future.sequence(Seq(
  45. Future({
  46. blocking {
  47. Thread.sleep(5000)
  48. }
  49. System.err.println("A")
  50. 5
  51. }),
  52. Future({
  53. blocking {
  54. Thread.sleep(7000)
  55. }
  56. System.err.println("B")
  57. 7
  58. //throw new Exception("Ha!")
  59. }),
  60. Future({
  61. blocking {
  62. Thread.sleep(9000)
  63. }
  64. System.err.println("C")
  65. 9
  66. }))),
  67. Duration("100 sec"))
  68.  
  69. System.err.println(a)
  70. } catch {
  71. case e: Exception ⇒
  72. e.printStackTrace()
  73. }
  74.  
  75. def lift[T](f: Future[T])(implicit ec: ExecutionContext): Future[Try[T]] =
  76. f map { Success(_) } recover { case e => Failure(e) }
  77.  
  78. def lift[T](fs: Seq[Future[T]])(implicit ec: ExecutionContext): Seq[Future[Try[T]]] =
  79. fs map { lift(_) }
  80.  
  81. implicit class RichSeqFuture[+T](val fs: Seq[Future[T]]) extends AnyVal {
  82. def onComplete[U](f: Seq[Try[T]] => U)(implicit ec: ExecutionContext) = {
  83. Future.sequence(lift(fs)) onComplete {
  84. case Success(s) => f(s)
  85. case Failure(e) => throw e // will never happen, because of the Try lifting
  86. }
  87. }
  88. }
  89.  
  90. val f1 = Future {
  91. throw new Throwable("baaa") // emulating a future that bumped into an exception
  92. }
  93.  
  94. val f2 = Future {
  95. Thread.sleep(3000L) // emulating a future that takes a bit longer to complete
  96. 2
  97. }
  98.  
  99. val lf = List(f1, f2)
  100.  
  101. lf onComplete { _ map {
  102. case Success(v) => ???
  103. case Failure(e) => ???
  104. }}
Add Comment
Please, Sign In to add comment