/// <summary> /// Projects the wrapped value into a new wrapper and invokes a result selector function on it. /// </summary> /// <typeparam name="TSource">The type of the wrapped value.</typeparam> /// <typeparam name="TIntermediate">The type to project the value into.</typeparam> /// <typeparam name="TResult">The type of the selected value.</typeparam> /// <param name="source">The wrapper.</param> /// <param name="project">A transform function to apply on the wrapped value.</param> /// <param name="select">A result selector function to apply on the projected value.</param> /// <returns>The selected value wrapped.</returns> public static Try <TResult> SelectMany <TSource, TIntermediate, TResult>( this Try <TSource> source, Func <TSource, Try <TIntermediate> > project, Func <TSource, TIntermediate, TResult> select) => source.Bind(sourceVal => project(sourceVal).Select(interVal => select(sourceVal, interVal)));
/// <summary> /// Evaluates a condition on the wrapped value. /// </summary> /// <typeparam name="TSource">The type of the wrapped value.</typeparam> /// <param name="source">The wrapper.</param> /// <param name="predicate">A function to test the wrapped value for a condition.</param> /// <returns> /// <paramref name="source"/> if <paramref name="predicate"/> yields true when evaluated on the wrapped value; a wrapped <see cref="InvalidOperationException"/> otherwise. /// </returns> public static Try <TSource> Where <TSource>(this Try <TSource> source, Func <TSource, bool> predicate) => source.Bind(val => predicate(val) ? source : Try.LiftException <TSource>(new InvalidOperationException("Predicate not satisfied")));
/// <summary> /// Projects the wrapped value into a new wrapper. /// </summary> /// <typeparam name="TSource">The type of the wrapped value.</typeparam> /// <typeparam name="TResult">The type to project the value into.</typeparam> /// <param name="source">The wrapper.</param> /// <param name="project">A transform function to apply on the wrapped value.</param> /// <returns>The projection of the wrapped value.</returns> public static Try <TResult> SelectMany <TSource, TResult>(this Try <TSource> source, Func <TSource, Try <TResult> > project) => source.Bind(val => project(val));
/// <summary> /// Projects the wrapped value into a new one and wraps it. /// </summary> /// <typeparam name="TSource">The type of the wrapped value.</typeparam> /// <typeparam name="TResult">The type to project the value into.</typeparam> /// <param name="source">The wrapper.</param> /// <param name="project">A transform function to apply on the wrapped value.</param> /// <returns>The projection of the wrapped value in a wrapper.</returns> public static Try <TResult> Select <TSource, TResult>(this Try <TSource> source, Func <TSource, TResult> project) => source.Bind(val => Try.LiftValue(project(val)));
/// <inheritdoc/> internal override Try <TResult> Bind <TResult>(Func <T, Try <TResult> > bind) => Try.LiftException <TResult>(this.Exception);
/// <inheritdoc/> public override Try <T> Recover(Func <Exception, T> recoverFn) => this.RecoverWith(ex => Try.LiftValue(recoverFn(ex)));
/// <summary> /// Maps a wrapped value to another mapped value. /// If this instance is an exception, <paramref name="map"/> is not invoked, and this method returns a wrapper for that exception. /// If <paramref name="map"/> throws an exception, this method returns a wrapper for that exception. /// </summary> /// <typeparam name="TResult">The type of the result of the mapping.</typeparam> /// <param name="map">The mapping function. Exceptions from this method are captured and wrapped.</param> /// <returns>The wrapped mapped value.</returns> public Try <TResult> Map <TResult>(Func <T, TResult> map) => this.Bind(value => Try.Create(() => map(value)));