@@ -426,21 +426,8 @@ trait Future[+T] extends Awaitable[T] {
426
426
* that conforms to `S`'s erased type or a `ClassCastException` otherwise.
427
427
*/
428
428
def mapTo [S ](implicit tag : ClassTag [S ]): Future [S ] = {
429
- import java .{ lang => jl }
430
- val toBoxed = Map [Class [_], Class [_]](
431
- classOf [Boolean ] -> classOf [jl.Boolean ],
432
- classOf [Byte ] -> classOf [jl.Byte ],
433
- classOf [Char ] -> classOf [jl.Character ],
434
- classOf [Short ] -> classOf [jl.Short ],
435
- classOf [Int ] -> classOf [jl.Integer ],
436
- classOf [Long ] -> classOf [jl.Long ],
437
- classOf [Float ] -> classOf [jl.Float ],
438
- classOf [Double ] -> classOf [jl.Double ],
439
- classOf [Unit ] -> classOf [scala.runtime.BoxedUnit ]
440
- )
441
-
442
429
def boxedType (c : Class [_]): Class [_] = {
443
- if (c.isPrimitive) toBoxed(c) else c
430
+ if (c.isPrimitive) Future . toBoxed(c) else c
444
431
}
445
432
446
433
val p = newPromise[S ]
@@ -530,7 +517,22 @@ trait Future[+T] extends Awaitable[T] {
530
517
* Note: using this method yields nondeterministic dataflow programs.
531
518
*/
532
519
object Future {
533
- /** Starts an asynchronous computation and returns a `Future` object with the result of that computation.
520
+
521
+ import java .{ lang => jl }
522
+
523
+ private [concurrent] val toBoxed = Map [Class [_], Class [_]](
524
+ classOf [Boolean ] -> classOf [jl.Boolean ],
525
+ classOf [Byte ] -> classOf [jl.Byte ],
526
+ classOf [Char ] -> classOf [jl.Character ],
527
+ classOf [Short ] -> classOf [jl.Short ],
528
+ classOf [Int ] -> classOf [jl.Integer ],
529
+ classOf [Long ] -> classOf [jl.Long ],
530
+ classOf [Float ] -> classOf [jl.Float ],
531
+ classOf [Double ] -> classOf [jl.Double ],
532
+ classOf [Unit ] -> classOf [scala.runtime.BoxedUnit ]
533
+ )
534
+
535
+ /** Starts an asynchronous computation and returns a `Future` object with the result of that computation.
534
536
*
535
537
* The result becomes available once the asynchronous computation is completed.
536
538
*
@@ -544,18 +546,18 @@ object Future {
544
546
import scala .collection .mutable .Builder
545
547
import scala .collection .generic .CanBuildFrom
546
548
547
- /** Simple version of `Futures.traverse`. Transforms a `Traversable [Future[A]]` into a `Future[Traversable [A]]`.
549
+ /** Simple version of `Futures.traverse`. Transforms a `TraversableOnce [Future[A]]` into a `Future[TraversableOnce [A]]`.
548
550
* Useful for reducing many `Future`s into a single `Future`.
549
551
*/
550
- def sequence [A , M [_] <: Traversable [_]](in : M [Future [A ]])(implicit cbf : CanBuildFrom [M [Future [A ]], A , M [A ]], executor : ExecutionContext ): Future [M [A ]] = {
552
+ def sequence [A , M [_] <: TraversableOnce [_]](in : M [Future [A ]])(implicit cbf : CanBuildFrom [M [Future [A ]], A , M [A ]], executor : ExecutionContext ): Future [M [A ]] = {
551
553
in.foldLeft(Promise .successful(cbf(in)).future) {
552
554
(fr, fa) => for (r <- fr; a <- fa.asInstanceOf [Future [A ]]) yield (r += a)
553
555
} map (_.result)
554
556
}
555
557
556
558
/** Returns a `Future` to the result of the first future in the list that is completed.
557
559
*/
558
- def firstCompletedOf [T ](futures : Traversable [Future [T ]])(implicit executor : ExecutionContext ): Future [T ] = {
560
+ def firstCompletedOf [T ](futures : TraversableOnce [Future [T ]])(implicit executor : ExecutionContext ): Future [T ] = {
559
561
val p = Promise [T ]()
560
562
561
563
val completeFirst : Either [Throwable , T ] => Unit = p tryComplete _
@@ -566,7 +568,8 @@ object Future {
566
568
567
569
/** Returns a `Future` that will hold the optional result of the first `Future` with a result that matches the predicate.
568
570
*/
569
- def find [T ](futures : Traversable [Future [T ]])(predicate : T => Boolean )(implicit executor : ExecutionContext ): Future [Option [T ]] = {
571
+ def find [T ](futurestravonce : TraversableOnce [Future [T ]])(predicate : T => Boolean )(implicit executor : ExecutionContext ): Future [Option [T ]] = {
572
+ val futures = futurestravonce.toBuffer
570
573
if (futures.isEmpty) Promise .successful[Option [T ]](None ).future
571
574
else {
572
575
val result = Promise [Option [T ]]()
@@ -577,8 +580,9 @@ object Future {
577
580
case _ =>
578
581
}
579
582
} finally {
580
- if (ref.decrementAndGet == 0 )
583
+ if (ref.decrementAndGet == 0 ) {
581
584
result tryComplete Right (None )
585
+ }
582
586
}
583
587
584
588
futures.foreach(_ onComplete search)
@@ -597,7 +601,7 @@ object Future {
597
601
* val result = Await.result(Future.fold(futures)(0)(_ + _), 5 seconds)
598
602
* }}}
599
603
*/
600
- def fold [T , R ](futures : Traversable [Future [T ]])(zero : R )(foldFun : (R , T ) => R )(implicit executor : ExecutionContext ): Future [R ] = {
604
+ def fold [T , R ](futures : TraversableOnce [Future [T ]])(zero : R )(foldFun : (R , T ) => R )(implicit executor : ExecutionContext ): Future [R ] = {
601
605
if (futures.isEmpty) Promise .successful(zero).future
602
606
else sequence(futures).map(_.foldLeft(zero)(foldFun))
603
607
}
@@ -609,20 +613,20 @@ object Future {
609
613
* val result = Await.result(Futures.reduce(futures)(_ + _), 5 seconds)
610
614
* }}}
611
615
*/
612
- def reduce [T , R >: T ](futures : Traversable [Future [T ]])(op : (R , T ) => R )(implicit executor : ExecutionContext ): Future [R ] = {
616
+ def reduce [T , R >: T ](futures : TraversableOnce [Future [T ]])(op : (R , T ) => R )(implicit executor : ExecutionContext ): Future [R ] = {
613
617
if (futures.isEmpty) Promise [R ].failure(new NoSuchElementException (" reduce attempted on empty collection" )).future
614
618
else sequence(futures).map(_ reduceLeft op)
615
619
}
616
620
617
- /** Transforms a `Traversable [A]` into a `Future[Traversable [B]]` using the provided function `A => Future[B]`.
621
+ /** Transforms a `TraversableOnce [A]` into a `Future[TraversableOnce [B]]` using the provided function `A => Future[B]`.
618
622
* This is useful for performing a parallel map. For example, to apply a function to all items of a list
619
623
* in parallel:
620
624
*
621
625
* {{{
622
626
* val myFutureList = Future.traverse(myList)(x => Future(myFunc(x)))
623
627
* }}}
624
628
*/
625
- def traverse [A , B , M [_] <: Traversable [_]](in : M [A ])(fn : A => Future [B ])(implicit cbf : CanBuildFrom [M [A ], B , M [B ]], executor : ExecutionContext ): Future [M [B ]] =
629
+ def traverse [A , B , M [_] <: TraversableOnce [_]](in : M [A ])(fn : A => Future [B ])(implicit cbf : CanBuildFrom [M [A ], B , M [B ]], executor : ExecutionContext ): Future [M [B ]] =
626
630
in.foldLeft(Promise .successful(cbf(in)).future) { (fr, a) =>
627
631
val fb = fn(a.asInstanceOf [A ])
628
632
for (r <- fr; b <- fb) yield (r += b)
0 commit comments