-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from mta-solutions/async-extensions
Remove internal scoping and add Async extension methods for validation contexts
- Loading branch information
Showing
7 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/FSharp.Data.Validation.Async/FSharp.Data.Validation.Async.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="VCtx.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FSharp.Data.Validation\FSharp.Data.Validation.fsproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
namespace FSharp.Data.Validation | ||
|
||
[<RequireQualifiedAccess>] | ||
module VCtx = | ||
/// <summary> | ||
/// Binds a function that returns an asynchronous computation to a validation context. | ||
/// </summary> | ||
/// <remarks> | ||
/// This function takes a function <c>fn</c> that transforms a value of type <c>'A</c> into an | ||
/// asynchronous computation of type <c>Async<VCtx<'F, 'B>></c> and a validation context <c>c</c> | ||
/// of type <c>VCtx<'F, 'A></c>. It returns an asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>. | ||
/// | ||
/// The function handles the following cases: | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description><c>ValidCtx a</c>: Applies the function <c>fn</c> to <c>a</c> and returns the result.</description> | ||
/// </item> | ||
/// <item> | ||
/// <description><c>RefutedCtx (gfs, lfs)</c>: Returns the same <c>RefutedCtx</c> without applying the function.</description> | ||
/// </item> | ||
/// <item> | ||
/// <description><c>DisputedCtx (gfs, lfs, a)</c>: Applies the function <c>fn</c> to <c>a</c> and merges the results accordingly.</description> | ||
/// </item> | ||
/// </list> | ||
/// </remarks> | ||
/// <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> | ||
/// <param name="c">A validation context of type <c>VCtx<'F, 'A></c>.</param> | ||
/// <returns>An asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>.</returns> | ||
let bindToAsync (fn:'A -> Async<VCtx<'F, 'B>>) (c: VCtx<'F, 'A>): Async<VCtx<'F, 'B>> = | ||
async { | ||
match c with | ||
| ValidCtx a -> return! fn a | ||
| RefutedCtx (gfs,lfs) -> return RefutedCtx (gfs,lfs) | ||
| DisputedCtx (gfs,lfs,a) -> | ||
let! b = fn a | ||
match b with | ||
| ValidCtx b -> return DisputedCtx (gfs,lfs,b) | ||
| DisputedCtx (gfs',lfs',b) -> return DisputedCtx (gfs @ gfs', Utilities.mergeFailures lfs lfs', b) | ||
| RefutedCtx (gfs',lfs') -> return RefutedCtx (gfs @ gfs', Utilities.mergeFailures lfs lfs') | ||
} | ||
|
||
/// <summary> | ||
/// Binds a function that returns a validation context to an asynchronous computation. | ||
/// </summary> | ||
/// <remarks> | ||
/// This function takes a function <c>fn</c> that transforms a value of type <c>'A</c> into a validation context | ||
/// of type <c>VCtx<'F, 'B></c> and an asynchronous computation <c>c</c> of type <c>Async<VCtx<'F, 'A>></c>. | ||
/// It returns an asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>. | ||
/// </remarks> | ||
/// <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> | ||
/// <param name="c">An asynchronous computation of type <c>Async<VCtx<'F, 'A>></c>.</param> | ||
/// <returns>An asynchronous computation of type <c>Async<VCtx<'F, 'B>></c>.</returns> | ||
let bindAsync (fn:'A -> Async<VCtx<'F, 'B>>) (c: Async<VCtx<'F, 'A>>): Async<VCtx<'F, 'B>> = | ||
async { | ||
let! c' = c | ||
return! bindToAsync fn c' | ||
} | ||
|
||
/// <summary> | ||
/// Binds a function that returns a validation context to a validation context. | ||
/// </summary> | ||
/// <remarks> | ||
/// This function takes a function <c>fn</c> that transforms a value of type <c>'A</c> into a validation context | ||
/// of type <c>VCtx<'F, 'B></c> and a validation context <c>c</c> of type <c>VCtx<'F, 'A></c>. | ||
/// It returns a validation context of type <c>VCtx<'F, 'B></c>. | ||
/// </remarks> | ||
/// <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> | ||
/// <param name="c">A validation context of type <c>VCtx<'F, 'A></c>.</param> | ||
/// <returns>A validation context of type <c>VCtx<'F, 'B></c>.</returns> | ||
let bindFromAsync (fn:'A -> VCtx<'F, 'B>) (c: Async<VCtx<'F, 'A>>): Async<VCtx<'F, 'B>> = | ||
bindAsync (fn >> async.Return) c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters