Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.78 KB | None | 0 0
  1.  
  2. import scala.concurrent._
  3. import ExecutionContext.Implicits.global
  4. import scala.util.{Success, Failure}
  5.  
  6. object Demo {
  7.     def main(args: Array[String]){
  8.  
  9.         // ---------------------------------------------------
  10.         // This shows taking a value of a future and operating on it to give another future.
  11.  
  12.         val b2: Future[List[Int]] = Future {
  13.             List(1,2)
  14.         }
  15.  
  16.         val c2:Future[List[Int]] = b2.map( x => x.map( _*3 ) )
  17.  
  18.         // Note how in this case the two `map` do not have the same meaning.
  19.  
  20.         c2 onComplete {
  21.           case Success(value) => println(s"Value: $value")
  22.           case Failure(t) => println("An error has occured: " + t.getMessage)
  23.         }
  24.  
  25.         // Outputs: Value: 2
  26.  
  27.         Thread.sleep(2000)
  28.  
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement