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 ))); }
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 ))); }
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)) ))); }
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)); } }
public void WhenHasCompleted() { new[] { Future.unfulfilled <int>(), Future.unfulfilled <int>(), Future.successful(1), Future.unfulfilled <int>(), Future.unfulfilled <int>() }.firstOf().value.get.shouldEqual(1); }
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); }
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); }
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()); })); }
public static Future <A> successful <A>(A value) => Future <A> .successful(value);
public void WhenSome() => F.some(Future.successful(3)).extractOpt().shouldEqual(Future.successful(F.some(3)));
public void WhenSome() { var f = Future.successful(3); F.some(f).extract().shouldEqual(f); }
public void CompleteToComplete() { Future.successful(3).collect(i => F.some(i * 2)).shouldBeCompleted(6); }
public void CompleteToNotComplete() { Future.successful(3).collect(i => F.none <int>()).shouldNotBeCompleted(); }
public void CompleteToComplete() { Future.successful(3).filter(i => true).shouldBeCompleted(3); }
public void CompleteToNotComplete() { Future.successful(3).filter(i => false).shouldNotBeCompleted(); }
public void WhenBothSidesSuccessful() { Future.successful(1).zip(Future.successful(2)).shouldBeOfSuccessfulType(F.t(1, 2)); }
public static Future <Option <A> > extractOpt <A>(this Option <Future <A> > futureOpt) => futureOpt.fold(() => Future.successful(F.none <A>()), f => f.map(F.some));
public void WhenSuccessful() { Future <int> .successful(1).map(mapper).shouldBeOfSuccessfulType(2); }
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)))) );
public static Future <A> successful <A>(A value) { return(Future <A> .successful(value)); }