-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use FsToolkit.ErrorHandling and reorganise modules (#5)
* Add rider files to gitignore * Increment version number * Reorganise modules * Remove task functions included in FsT
- Loading branch information
1 parent
274fb6c
commit 1994665
Showing
26 changed files
with
419 additions
and
407 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ obj/ | |
release.cmd | ||
release.sh | ||
.ionide/ | ||
.idea/ | ||
_public | ||
build/ | ||
out/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace AsyncWriterResult | ||
|
||
[<RequireQualifiedAccess>] | ||
module Async = | ||
|
||
let zip c1 c2 = | ||
async { | ||
let! ct = Async.CancellationToken | ||
let x = Async.StartImmediateAsTask (c1, cancellationToken = ct) | ||
let y = Async.StartImmediateAsTask (c2, cancellationToken = ct) | ||
let! x' = Async.AwaitTask x | ||
let! y' = Async.AwaitTask y | ||
return x', y' | ||
} |
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,11 @@ | ||
namespace AsyncWriterResult | ||
|
||
open FsToolkit.ErrorHandling | ||
|
||
[<RequireQualifiedAccess>] | ||
module AsyncWriter = | ||
|
||
let retn a = Writer.retn a |> Async.retn | ||
|
||
let map f = f |> Writer.map |> Async.map | ||
|
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,81 @@ | ||
namespace AsyncWriterResult | ||
|
||
open FsToolkit.ErrorHandling | ||
open AsyncWriterResult | ||
|
||
type AsyncWriterResult<'ok, 'error, 'log> = Async<Writer<'log list, Result<'ok, 'error>>> | ||
|
||
[<RequireQualifiedAccess>] | ||
module AsyncWriterResult = | ||
|
||
let retn x = x |> WriterResult.retn |> Async.retn | ||
|
||
let map f = f |> WriterResult.map |> Async.map | ||
|
||
// let bind (f:'a -> Async<Writer<'b list, Result<'c,'d>>>) (m:Async<Writer<'b list, Result<'a,'d>>>) : Async<Writer<'b list, Result<'c,'d>>> = async { | ||
let bind f m = | ||
async { | ||
let! w = m | ||
let r, logs1 = Writer.run w | ||
|
||
match r with | ||
| Ok a -> | ||
let! ww = f a | ||
let b, logs2 = Writer.run ww | ||
return Writer <| fun () -> b, logs1 @ logs2 | ||
| Error e -> return Writer <| fun () -> Error e, logs1 | ||
} | ||
|
||
let apply f m = | ||
async { | ||
let! uf = f | ||
let! um = m | ||
let r1, logs1 = Writer.run uf | ||
let r2, logs2 = Writer.run um | ||
|
||
match r1, r2 with | ||
| Ok g, Ok h -> return Writer <| fun () -> Ok(g h), logs1 @ logs2 | ||
| Error e1, _ -> return Writer <| fun () -> Error e1, logs1 @ logs2 | ||
| _, Error e2 -> return Writer <| fun () -> Error e2, logs1 @ logs2 | ||
} | ||
|
||
let write log = | ||
async { return Writer(fun () -> Result.retn (), [ log ]) } | ||
|
||
let mapError e m = | ||
async { | ||
let! w = m | ||
let r, logs = Writer.run w | ||
return Writer <| fun () -> Result.mapError e r, logs | ||
} | ||
|
||
let private errMsg m (e: exn) = Error(sprintf "%s: %s" m e.Message) | ||
|
||
let tryTo desc f = | ||
f | ||
>> Async.Catch | ||
>> Async.map (function | ||
| Choice1Of2 a -> Ok a | ||
| Choice2Of2 e when (e :? System.AggregateException) -> errMsg desc e.InnerException | ||
| Choice2Of2 e -> errMsg desc e) | ||
>> Async.map Writer.retn | ||
|
||
let traverseResultM f list = | ||
|
||
let (>>=) x f = bind f x | ||
|
||
let cons head tail = head :: tail | ||
|
||
let folder head tail = | ||
f head | ||
>>= (fun h -> tail >>= (fun t -> retn (cons h t))) | ||
|
||
List.foldBack folder list (retn []) | ||
|
||
let collect list = | ||
Async.Parallel list | ||
|> Async.map (List.ofArray >> WriterResult.collect) | ||
|
||
let zip left right = | ||
Async.zip left right | ||
|> Async.map (fun (r1, r2) -> WriterResult.zip r1 r2) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace AsyncWriterResult | ||
|
||
open FsToolkit.ErrorHandling | ||
open System.Threading.Tasks | ||
|
||
[<AutoOpen>] | ||
module AsyncWriterResultCE = | ||
type AsyncWriterResultBuilder() = | ||
member __.Return(x) = AsyncWriterResult.retn x | ||
member __.ReturnFrom(m: Async<Writer<'w, Result<'a, 'b>>>) = m | ||
member __.Bind(m, f) = AsyncWriterResult.bind f m | ||
member __.Zero() = __.Return() | ||
member __.BindReturn(x, f) = AsyncWriterResult.map f x | ||
member __.MergeSources(x, y) = AsyncWriterResult.zip x y | ||
member __.Source(x: Async<Writer<'w, Result<'a, 'b>>>) = x | ||
member __.Source(x: Task<Writer<'w, Result<'a, 'b>>>) = x |> Async.AwaitTask | ||
|
||
let asyncWriterResult = AsyncWriterResultBuilder() | ||
|
||
[<AutoOpen>] | ||
module AsyncWriterResultBuilderExtensions = | ||
type AsyncWriterResultBuilder with | ||
member __.Source(x: Result<'a, 'b>) = x |> AsyncWriter.retn | ||
member __.Source(x: Writer<'w, 't>) = x |> Writer.map Ok |> Async.retn | ||
member __.Source(x: Async<'t>) = x |> Async.map WriterResult.retn | ||
member __.Source(x: Task<'t>) = x |> Async.AwaitTask |> Async.map WriterResult.retn | ||
|
||
[<AutoOpen>] | ||
module AsyncWriterResultBuilderExtensionsHighPriority = | ||
type AsyncWriterResultBuilder with | ||
member __.Source(x: Writer<'w, Result<'a, 'b>>) = x |> Async.retn | ||
member __.Source(x: Async<Result<'a, 'b>>) = x |> Async.map Writer.retn | ||
member __.Source(x: Async<Writer<'w, 't>>) = x |> AsyncWriter.map Result.retn | ||
member __.Source(x: Task<Result<'a, 'b>>) = x |> Async.AwaitTask |> Async.map Writer.retn | ||
member __.Source(x: Task<Writer<'w, 't>>) = x |> Async.AwaitTask |> AsyncWriter.map Result.retn |
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,15 @@ | ||
namespace AsyncWriterResult.Operator.AsyncWriterResult | ||
|
||
open AsyncWriterResult | ||
|
||
/// Operators for working with the AsyncWriterResult type | ||
[<AutoOpen>] | ||
module AsyncWriterResult = | ||
/// AsyncWriterResult.map | ||
let (<!>) f x = AsyncWriterResult.map f x | ||
|
||
/// AsyncWriterResult.apply | ||
let (<*>) f x = AsyncWriterResult.apply f x | ||
|
||
/// AsyncWriterResult.bind | ||
let (>>=) x f = AsyncWriterResult.bind f x |
Oops, something went wrong.