// TODO: TEST /// <summary> /// Empties an optional, if a specified predicate /// is not satisfied. /// </summary> /// <param name="predicate">The predicate.</param> /// <returns>The filtered optional.</returns> public AsyncOption <T> Filter(Func <T, Task <bool> > predicate) { return(InnerTask.FlatMap(option => option .Match( some: value => predicate(value).Map(option.Filter), none: () => Task.FromResult(option) )).ToAsyncOption()); }
/// <summary> /// Transforms the inner value in an async optional /// into another async optional. The result is flattened, /// and if either is empty, an empty optional is returned. /// </summary> /// <param name="mapping">The transformation function.</param> /// <returns>The transformed optional.</returns> public AsyncOption <TResult> FlatMap <TResult>(Func <T, Task <Option <TResult> > > mapping) { return(InnerTask.FlatMap(option => option .Match( some: mapping, none: () => Task.FromResult(Option.None <TResult>()) )).ToAsyncOption()); }
// TODO: TEST /// <summary> /// Determines if the current optional contains a specified value. /// </summary> /// <param name="value">The value to locate.</param> /// <returns>A boolean indicating whether or not the value was found.</returns> public Task <bool> Contains(Task <T> value) { return(InnerTask.FlatMap(option => value.Map(option.Contains))); }
// TODO: TEST /// <summary> /// Empties an optional, if a specified condition /// is not satisfied. /// </summary> /// <param name="condition">The condition.</param> /// <returns>The filtered optional.</returns> public AsyncOption <T> Filter(Task <bool> condition) { return(InnerTask.FlatMap(option => condition.Map(option.Filter)).ToAsyncOption()); }
// TODO: TEST /// <summary> /// Evaluates a specified action, based on whether a value is present or not. /// </summary> /// <param name="some">The action to evaluate if the value is present.</param> /// <param name="none">The action to evaluate if the value is missing.</param> public Task Match(Func <T, Task> some, Func <Task> none) { return(InnerTask.FlatMap(option => option.Match(some, none))); }
// TODO: TEST /// <summary> /// Evaluates a specified function, based on whether a value is present or not. /// </summary> /// <param name="some">The function to evaluate if the value is present.</param> /// <param name="none">The function to evaluate if the value is missing.</param> /// <returns>The result of the evaluated function.</returns> public Task <TResult> Match <TResult>(Func <T, Task <TResult> > some, Func <Task <TResult> > none) { return(InnerTask.FlatMap(option => option.Match(some, none))); }