Exemple #1
0
 public static Future <Either <Err, To> > flatMapE <From, To, Err>(
     this Future <Either <Err, From> > future, Fn <From, Future <Either <Err, To> > > mapper
     )
 {
     return(future.flatMap(e => e.fold(
                               err => Future.successful(Either <Err, To> .Left(err)),
                               mapper
                               )));
 }
Exemple #2
0
 public static Future <Option <B> > flatMapO <A, B>(
     this Future <Option <A> > future, Fn <A, Future <Option <B> > > mapper
     )
 {
     return(future.flatMap(opt => opt.fold(
                               () => Future.successful(F.none <B>()),
                               mapper
                               )));
 }
Exemple #3
0
 public static Future <Try <To> > flatMapT <From, To>(
     this Future <Try <From> > future, Fn <From, Future <To> > mapper
     )
 {
     return(future.flatMap(t => t.fold(
                               from => mapper(from).map(F.scs),
                               err => Future.successful(F.err <To>(err))
                               )));
 }
Exemple #4
0
 public void WhenEitherSideUnfulfilled()
 {
     foreach (var t in new[] {
         F.t("X-O", Future.unfulfilled <int>(), Future.successful(1)),
         F.t("O-X", Future.successful(1), Future.unfulfilled <int>())
     })
     {
         t.ua((name, fa, fb) => fa.zip(fb).shouldBeOfUnfulfilledType(name));
     }
 }
Exemple #5
0
 public void WhenHasCompleted()
 {
     new[] {
         Future.unfulfilled <int>(),
         Future.unfulfilled <int>(),
         Future.successful(1),
         Future.unfulfilled <int>(),
         Future.unfulfilled <int>()
     }.firstOf().value.get.shouldEqual(1);
 }
Exemple #6
0
        public void Equals()
        {
            Promise <int> asyncP;
            var           asyncF = Future <int> .async(out asyncP);

            var unfullfilled = Future.unfulfilled <int>();
            var completed    = Future.successful(3);

            shouldNotEqualSymmetrical(unfullfilled, completed);

            shouldBeIdentical(unfullfilled, asyncF);
            shouldNotEqualSymmetrical(asyncF, completed);
            asyncP.complete(3);
            shouldNotEqualSymmetrical(unfullfilled, asyncF);
            shouldBeIdentical(asyncF, completed);
        }
Exemple #7
0
        public void ASyncToSuccessful()
        {
            Promise <int> p;
            var           f = Future <int> .async(out p);

            var called = false;
            var f2     = f.flatMap(i => {
                called = true;
                return(Future.successful(i));
            });

            f2.type.shouldEqual(FutureType.ASync);
            f2.value.shouldBeNone();
            called.shouldBeFalse("it should not call function until completion of a source promise");
            p.complete(1);
            called.shouldBeTrue();
            f2.value.shouldBeSome(1);
        }
Exemple #8
0
        public Future <C> zip <B, C>(Future <B> fb, Fn <A, B, C> mapper)
        {
            if (implementation.isB || fb.implementation.isB)
            {
                return(Future <C> .unfulfilled);
            }
            if (implementation.isA && fb.implementation.isA)
            {
                return(Future.successful(mapper(implementation.__unsafeGetA, fb.implementation.__unsafeGetA)));
            }

            var fa = this;

            return(Future <C> .async(p => {
                Act tryComplete = () => fa.value.zip(fb.value, mapper).each(ab => p.tryComplete(ab));
                fa.onComplete(a => tryComplete());
                fb.onComplete(b => tryComplete());
            }));
        }
Exemple #9
0
 public static Future <A> successful <A>(A value) => Future <A> .successful(value);
Exemple #10
0
 public void WhenSome() =>
 F.some(Future.successful(3)).extractOpt().shouldEqual(Future.successful(F.some(3)));
Exemple #11
0
        public void WhenSome()
        {
            var f = Future.successful(3);

            F.some(f).extract().shouldEqual(f);
        }
Exemple #12
0
 public void CompleteToComplete()
 {
     Future.successful(3).collect(i => F.some(i * 2)).shouldBeCompleted(6);
 }
Exemple #13
0
 public void CompleteToNotComplete()
 {
     Future.successful(3).collect(i => F.none <int>()).shouldNotBeCompleted();
 }
Exemple #14
0
 public void CompleteToComplete()
 {
     Future.successful(3).filter(i => true).shouldBeCompleted(3);
 }
Exemple #15
0
 public void CompleteToNotComplete()
 {
     Future.successful(3).filter(i => false).shouldNotBeCompleted();
 }
Exemple #16
0
 public void WhenBothSidesSuccessful()
 {
     Future.successful(1).zip(Future.successful(2)).shouldBeOfSuccessfulType(F.t(1, 2));
 }
Exemple #17
0
 public static Future <Option <A> > extractOpt <A>(this Option <Future <A> > futureOpt) =>
 futureOpt.fold(() => Future.successful(F.none <A>()), f => f.map(F.some));
Exemple #18
0
 public void WhenSuccessful()
 {
     Future <int> .successful(1).map(mapper).shouldBeOfSuccessfulType(2);
 }
Exemple #19
0
 public Future <B> map <B>(Fn <A, B> mapper) => implementation.fold(
     v => Future <B> .successful(mapper(v)),
     _ => Future <B> .unfulfilled,
     f => Future <B> .async(p => f.onComplete(v => p.complete(mapper(v))))
     );
Exemple #20
0
 public static Future <A> successful <A>(A value)
 {
     return(Future <A> .successful(value));
 }