internal static Outcome <TSource> ReduceImpl <TSource>( this IEnumerable <TSource> source, Func <TSource, TSource, Outcome <TSource> > accumulator, Func <Outcome <TSource>, bool> predicate) { Debug.Assert(source != null); Debug.Assert(accumulator != null); Debug.Assert(predicate != null); using (var iter = source.GetEnumerator()) { if (!iter.MoveNext()) { throw new InvalidOperationException("Source sequence was empty."); } Outcome <TSource> retval = Outcome <TSource> .η(iter.Current); while (predicate(retval) && iter.MoveNext()) { retval = retval.Bind(val => accumulator(val, iter.Current)); } return(retval); } }
public void Compose_Functions_ShouldResultFailureOutcome() { // Act PossibleBe <CustomerTest> possibleBeCustomer = new CustomerTest(); Outcome <CustomerTest> outcomeCustomer = possibleBeCustomer.TranslateToOutcome("error"); var outcomeResult = outcomeCustomer .Bind(outcome => Test(outcome)) .Bind(outcome2 => Test3(outcome2)) .WhenFailDo(outcome => Log(outcome.ErrorMessages.First())) .Return(x => x.Success ? 1 : -1); outcomeResult .Should() .Be(-1); }
internal static Outcome <TAccumulate> FoldImpl <TSource, TAccumulate>( this IEnumerable <TSource> source, TAccumulate seed, Func <TAccumulate, TSource, Outcome <TAccumulate> > accumulator) { Debug.Assert(source != null); Debug.Assert(accumulator != null); Outcome <TAccumulate> retval = Outcome <TAccumulate> .η(seed); using (var iter = source.GetEnumerator()) { while (iter.MoveNext()) { retval = retval.Bind(val => accumulator(val, iter.Current)); } } return(retval); }
public void Compose_Functions_ShouldResultSuccessullyOutcome() { // Act PossibleBe <CustomerTest> possibleBeCustomer = new CustomerTest(); Outcome <CustomerTest> outcomeCustomer = possibleBeCustomer.TranslateToOutcome("error"); var outcomeResult = outcomeCustomer .Bind(firstParameter => Test(firstParameter)) .Bind(parameterFromPreviousExecution => Test2(parameterFromPreviousExecution)); outcomeResult.Failure .Should() .BeFalse(); outcomeResult.ErrorMessages .Should() .NotBeNull() .And .HaveCount(0); outcomeResult.Success .Should() .BeTrue(); }