Permalink
Browse files

Add methods to Future for map, flatMap, and foreach

  • Loading branch information...
1 parent 147edfe commit e0cb666edc240c28dd1a16775cde0d33b77b9d85 @derekjw derekjw committed Feb 22, 2011
Showing with 18 additions and 0 deletions.
  1. +18 −0 akka-actor/src/main/scala/akka/dispatch/Future.scala
@@ -180,6 +180,24 @@ sealed trait Future[T] {
}
}
+ final def map[A](f: T => A): Future[A] = {
+ val fa = new DefaultCompletableFuture[A](timeoutInNanos, NANOS)
+ onComplete (_.value.foreach(_.fold(fa.completeWithException, r => fa.complete(try { Right(f(r)) } catch { case e => Left(e) }))))
+ fa
+ }
+
+ final def flatMap[A](f: T => Future[A]): Future[A] = {
+ val fa = new DefaultCompletableFuture[A](timeoutInNanos, NANOS)
+ onComplete (_.value.foreach(_.fold(fa.completeWithException, r => try { f(r).onComplete(fa.completeWith(_)) } catch { case e => fa.completeWithException(e) })))
+ fa
+ }
+
+ final def foreach(f: T => Unit): Unit = onComplete { ft =>
+ val optr = ft.result
+ if (optr.isDefined)
+ f(optr.get)
+ }
+
/**
* Returns the current result, throws the exception is one has been raised, else returns None
*/

0 comments on commit e0cb666

Please sign in to comment.