|
| 1 | +namespace FSharp.Data.Validation |
| 2 | + |
| 3 | +[<RequireQualifiedAccess>] |
| 4 | +module VCtx = |
| 5 | + /// <summary> |
| 6 | + /// Binds a function that returns an asynchronous computation to a validation context. |
| 7 | + /// </summary> |
| 8 | + /// <remarks> |
| 9 | + /// This function takes a function <c>fn</c> that transforms a value of type <c>'A</c> into an |
| 10 | + /// asynchronous computation of type <c>Async<VCtx<'F, 'B>></c> and a validation context <c>c</c> |
| 11 | + /// of type <c>VCtx<'F, 'A></c>. It returns an asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>. |
| 12 | + /// |
| 13 | + /// The function handles the following cases: |
| 14 | + /// <list type="bullet"> |
| 15 | + /// <item> |
| 16 | + /// <description><c>ValidCtx a</c>: Applies the function <c>fn</c> to <c>a</c> and returns the result.</description> |
| 17 | + /// </item> |
| 18 | + /// <item> |
| 19 | + /// <description><c>RefutedCtx (gfs, lfs)</c>: Returns the same <c>RefutedCtx</c> without applying the function.</description> |
| 20 | + /// </item> |
| 21 | + /// <item> |
| 22 | + /// <description><c>DisputedCtx (gfs, lfs, a)</c>: Applies the function <c>fn</c> to <c>a</c> and merges the results accordingly.</description> |
| 23 | + /// </item> |
| 24 | + /// </list> |
| 25 | + /// </remarks> |
| 26 | + /// <param name="fn">A function that takes a value of type <c>'A</c> and returns an asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>.</param> |
| 27 | + /// <param name="c">A validation context of type <c>VCtx<'F, 'A></c>.</param> |
| 28 | + /// <returns>An asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>.</returns> |
| 29 | + let bindToAsync (fn:'A -> Async<VCtx<'F, 'B>>) (c: VCtx<'F, 'A>): Async<VCtx<'F, 'B>> = |
| 30 | + async { |
| 31 | + match c with |
| 32 | + | ValidCtx a -> return! fn a |
| 33 | + | RefutedCtx (gfs,lfs) -> return RefutedCtx (gfs,lfs) |
| 34 | + | DisputedCtx (gfs,lfs,a) -> |
| 35 | + let! b = fn a |
| 36 | + match b with |
| 37 | + | ValidCtx b -> return DisputedCtx (gfs,lfs,b) |
| 38 | + | DisputedCtx (gfs',lfs',b) -> return DisputedCtx (gfs @ gfs', Utilities.mergeFailures lfs lfs', b) |
| 39 | + | RefutedCtx (gfs',lfs') -> return RefutedCtx (gfs @ gfs', Utilities.mergeFailures lfs lfs') |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Binds a function that returns a validation context to an asynchronous computation. |
| 44 | + /// </summary> |
| 45 | + /// <remarks> |
| 46 | + /// This function takes a function <c>fn</c> that transforms a value of type <c>'A</c> into a validation context |
| 47 | + /// of type <c>VCtx<'F, 'B></c> and an asynchronous computation <c>c</c> of type <c>Async<VCtx<'F, 'A>></c>. |
| 48 | + /// It returns an asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>. |
| 49 | + /// </remarks> |
| 50 | + /// <param name="fn">A function that takes a value of type <c>'A</c> and returns a validation context of type <c>VCtx<'F, 'B></c>.</param> |
| 51 | + /// <param name="c">An asynchronous computation of type <c>Async<VCtx<'F, 'A>></c>.</param> |
| 52 | + /// <returns>An asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>.</returns> |
| 53 | + let bindAsync (fn:'A -> Async<VCtx<'F, 'B>>) (c: Async<VCtx<'F, 'A>>): Async<VCtx<'F, 'B>> = |
| 54 | + async { |
| 55 | + let! c' = c |
| 56 | + return! bindToAsync fn c' |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Binds a function that returns a validation context to a validation context. |
| 61 | + /// </summary> |
| 62 | + /// <remarks> |
| 63 | + /// This function takes a function <c>fn</c> that transforms a value of type <c>'A</c> into a validation context |
| 64 | + /// of type <c>VCtx<'F, 'B></c> and a validation context <c>c</c> of type <c>VCtx<'F, 'A></c>. |
| 65 | + /// It returns a validation context of type <c>VCtx<'F, 'B></c>. |
| 66 | + /// </remarks> |
| 67 | + /// <param name="fn">A function that takes a value of type <c>'A</c> and returns a validation context of type <c>VCtx<'F, 'B></c>.</param> |
| 68 | + /// <param name="c">A validation context of type <c>VCtx<'F, 'A></c>.</param> |
| 69 | + /// <returns>A validation context of type <c>VCtx<'F, 'B></c>.</returns> |
| 70 | + let bindFromAsync (fn:'A -> VCtx<'F, 'B>) (c: Async<VCtx<'F, 'A>>): Async<VCtx<'F, 'B>> = |
| 71 | + bindAsync (fn >> async.Return) c |
0 commit comments