public static void Report() { var value = Result.Success(1); IResult <int> four = value .Map(Calculator.AddOne) .Map(Calculator.Square); four.Bind(i => CsvFile.TryWriteLine($"The answer is: {i}")); }
/// <summary> /// Executes the given function on the disposable type embedded in the extended Result object /// </summary> /// <typeparam name="TDisposable"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="this"></param> /// <param name="functionResult"></param> /// <returns></returns> public static IResult <TResult> UsingResult <TDisposable, TResult>(this IResult <TDisposable> @this, Func <TDisposable, IResult <TResult> > functionResult) where TDisposable : IDisposable => @this .Bind(disposable => disposable.Using(functionResult));
public static IResult <L, R2> FlatMap <L, R, R2>( this IResult <L, R> self, Func <R, IResult <L, R2> > f) { return(self.Bind(Failure.Create <L, R2>, f)); }
public static IResult <L, R2> Map <L, R, R2>(this IResult <L, R> self, Func <R, R2> f) { return(self.Bind(Failure.Create <L, R2>, r => Success.Create <L, R2>(f(r)))); }
public void Bind_WhenFirstEndWithFailure() => firstFailureResult.Bind(() => secondResult).Map(_ => throw new Exception("error"), x => x).Message.ShouldBe(error);
public void Bind_WhenFirstEndWithSuccess() => firstSuccessResult.Bind(() => secondResult).Map(x => x, _ => throw new Exception("error")).ShouldBe(second);
public static IResult <TC, TError> SelectMany <TA, TB, TC, TError>(this IResult <TA, TError> result, Func <TA, IResult <TB, TError> > function, Func <TA, TB, TC> composer) => result.Bind(v1 => function(v1).Select(v2 => composer(v1, v2)));
public static IResult <TB, TError> SelectMany <TA, TB, TError>(this IResult <TA, TError> result, Func <TA, IResult <TB, TError> > function) => result.Bind(function);
public static IResult <TB, TError> MapSuccess <TA, TB, TError>(this IResult <TA, TError> result, Func <TA, TB> function) => result.Bind(x => function(x).ToSuccess <TB, TError>());
public static IResult <TNext> BindAny <TPrev, TNext>(this IResult <TPrev> prev, Func <TPrev, TNext> next) => prev.Bind(a => next(a).AsResult());
public static IResult <Unit> ToUnit <T>(this IResult <T> result) => result.Bind(_ => ResultFactory.CreateSuccess());