|
| 1 | +namespace FsToolkit.ErrorHandling |
| 2 | + |
| 3 | +open System.Threading.Tasks |
| 4 | + |
| 5 | + |
| 6 | +[<RequireQualifiedAccess>] |
| 7 | +module TaskValueOption = |
| 8 | + |
| 9 | + let inline map ([<InlineIfLambda>] f) ar = Task.map (ValueOption.map f) ar |
| 10 | + |
| 11 | + let inline bind ([<InlineIfLambda>] f) (ar: Task<_>) = |
| 12 | + task { |
| 13 | + let! opt = ar |
| 14 | + |
| 15 | + let t = |
| 16 | + match opt with |
| 17 | + | ValueSome x -> f x |
| 18 | + | ValueNone -> task { return ValueNone } |
| 19 | + |
| 20 | + return! t |
| 21 | + } |
| 22 | + |
| 23 | + let inline valueSome x = task { return ValueSome x } |
| 24 | + |
| 25 | + let inline apply f x = |
| 26 | + bind (fun f' -> bind (fun x' -> valueSome (f' x')) x) f |
| 27 | + |
| 28 | + let inline zip x1 x2 = |
| 29 | + Task.zip x1 x2 |
| 30 | + |> Task.map (fun (r1, r2) -> ValueOption.zip r1 r2) |
| 31 | + |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Returns result of running <paramref name="onValueSome"/> if it is <c>ValueSome</c>, otherwise returns result of running <paramref name="onValueNone"/> |
| 35 | + /// </summary> |
| 36 | + /// <param name="onValueSome">The function to run if <paramref name="input"/> is <c>ValueSome</c></param> |
| 37 | + /// <param name="onValueNone">The function to run if <paramref name="input"/> is <c>ValueNone</c></param> |
| 38 | + /// <param name="input">The input voption.</param> |
| 39 | + /// <returns> |
| 40 | + /// The result of running <paramref name="onValueSome"/> if the input is <c>ValueSome</c>, else returns result of running <paramref name="onValueNone"/>. |
| 41 | + /// </returns> |
| 42 | + let inline either |
| 43 | + ([<InlineIfLambda>] onValueSome: 'input -> Task<'output>) |
| 44 | + ([<InlineIfLambda>] onValueNone: unit -> Task<'output>) |
| 45 | + (input: Task<'input voption>) |
| 46 | + : Task<'output> = |
| 47 | + input |
| 48 | + |> Task.bind ( |
| 49 | + function |
| 50 | + | ValueSome v -> onValueSome v |
| 51 | + | ValueNone -> onValueNone () |
| 52 | + ) |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets the value of the option if the option is <c>Some</c>, otherwise returns the specified default value. |
| 56 | + /// </summary> |
| 57 | + /// <param name="value">The specified default value.</param> |
| 58 | + /// <param name="taskValueOption">The input option.</param> |
| 59 | + /// <returns> |
| 60 | + /// The option if the option is <c>Some</c>, else the default value. |
| 61 | + /// </returns> |
| 62 | + let inline defaultValue (value: 'value) (taskValueOption: Task<'value voption>) = |
| 63 | + taskValueOption |
| 64 | + |> Task.map (ValueOption.defaultValue value) |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Gets the value of the voption if the voption is <c>ValueSome</c>, otherwise evaluates <paramref name="defThunk"/> and returns the result. |
| 68 | + /// </summary> |
| 69 | + /// <param name="defThunk">A thunk that provides a default value when evaluated.</param> |
| 70 | + /// <param name="taskValueOption">The input voption.</param> |
| 71 | + /// <returns> |
| 72 | + /// The voption if the option is <c>ValueSome</c>, else the result of evaluating <paramref name="defThunk"/>. |
| 73 | + /// </returns> |
| 74 | + let inline defaultWith |
| 75 | + ([<InlineIfLambda>] defThunk: unit -> 'value) |
| 76 | + (taskValueOption: Task<'value voption>) |
| 77 | + : Task<'value> = |
| 78 | + taskValueOption |
| 79 | + |> Task.map (ValueOption.defaultWith defThunk) |
0 commit comments