Advertisement
Guest User

Untitled

a guest
Jun 9th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.53 KB | None | 0 0
  1.   def par_fib_(n: Int): Long = {
  2.     def fibHelper(n: Int): Future[Long] =
  3.       if (n<=46) Future{ fib(n) } else {
  4.         val f1: Future[Long] = fibHelper(n-1)
  5.         val f2: Future[Long] = fibHelper(n-2)
  6.  
  7. //        Await.result(f1, Duration.Inf) + Await.result(f2, Duration.Inf)
  8.         for { nf1 <- f1
  9.               nf2 <- f2 } yield nf1 + nf2
  10.  
  11.         f1.flatMap( nf1 => f2.map ( nf2 => nf1 + nf2 ) )
  12.       }
  13.  
  14.     // NOTE: the 'join' form, converting Future[Long] to Long
  15.     Await.result(fibHelper(n), Duration.Inf)
  16.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement