From afa5dc08d815e5f80b8415c73e8f097a5a0ad704 Mon Sep 17 00:00:00 2001 From: Matthew Watt Date: Fri, 9 May 2025 12:45:22 -0500 Subject: [PATCH] Add ok and error helper functions to TaskResultOption and AsyncResultOption modules --- gitbook/SUMMARY.md | 2 ++ gitbook/asyncResultOption/error.md | 22 +++++++++++++++++++ gitbook/taskResultOption/error.md | 22 +++++++++++++++++++ .../AsyncResultOption.fs | 7 ++++++ .../TaskResultOption.fs | 4 ++++ 5 files changed, 57 insertions(+) create mode 100644 gitbook/asyncResultOption/error.md create mode 100644 gitbook/taskResultOption/error.md diff --git a/gitbook/SUMMARY.md b/gitbook/SUMMARY.md index ff859312..be7010a4 100644 --- a/gitbook/SUMMARY.md +++ b/gitbook/SUMMARY.md @@ -131,6 +131,7 @@ * [apply](asyncResultOption/apply.md) * [bind](asyncResultOption/bind.md) * [Computation Expression](asyncResultOption/ce.md) + * [error](asyncResultOption/error.md) * [ignore](asyncResultOption/ignore.md) * [map](asyncResultOption/map.md) * [map2](asyncResultOption/map2.md) @@ -196,6 +197,7 @@ * [apply](taskResultOption/apply.md) * [bind](taskResultOption/bind.md) * [Computation Expression](taskResultOption/ce.md) + * [error](taskResultOption/error.md) * [ignore](taskResultOption/ignore.md) * [map](taskResultOption/map.md) * [map2](taskResultOption/map2.md) diff --git a/gitbook/asyncResultOption/error.md b/gitbook/asyncResultOption/error.md new file mode 100644 index 00000000..2b766264 --- /dev/null +++ b/gitbook/asyncResultOption/error.md @@ -0,0 +1,22 @@ +## AsyncResultOption.error + +Namespace: `FsToolkit.ErrorHandling` + +Lift an `'error` value into an `Async>` + +## Function Signature: + +```fsharp +'error -> Async> +``` + +## Examples + +### Example 1 + + +```fsharp +let result : Async> = + AsyncResultOption.error "Something bad happened" +``` + diff --git a/gitbook/taskResultOption/error.md b/gitbook/taskResultOption/error.md new file mode 100644 index 00000000..739a3886 --- /dev/null +++ b/gitbook/taskResultOption/error.md @@ -0,0 +1,22 @@ +## TaskResultOption.error + +Namespace: `FsToolkit.ErrorHandling` + +Lift an `'error` value into an `Task>` + +## Function Signature: + +```fsharp +'error -> Task> +``` + +## Examples + +### Example 1 + + +```fsharp +let result : Task> = + TaskResultOption.error "Something bad happened" +``` + diff --git a/src/FsToolkit.ErrorHandling/AsyncResultOption.fs b/src/FsToolkit.ErrorHandling/AsyncResultOption.fs index f950e3cc..98f306aa 100644 --- a/src/FsToolkit.ErrorHandling/AsyncResultOption.fs +++ b/src/FsToolkit.ErrorHandling/AsyncResultOption.fs @@ -24,6 +24,13 @@ module AsyncResultOption = ) input + let inline ok x = + Ok(Some x) + |> Async.singleton + + let inline error x : Async> = + Error x + |> Async.singleton let inline map2 ([] mapper: 'okInput1 -> 'okInput2 -> 'okOutput) diff --git a/src/FsToolkit.ErrorHandling/TaskResultOption.fs b/src/FsToolkit.ErrorHandling/TaskResultOption.fs index 81dad95c..6b884f5f 100644 --- a/src/FsToolkit.ErrorHandling/TaskResultOption.fs +++ b/src/FsToolkit.ErrorHandling/TaskResultOption.fs @@ -23,6 +23,10 @@ module TaskResultOption = let inline singleton value = TaskResult.ok (Some value) + let inline ok x = singleton x + + let inline error x : TaskResult<'ok option, 'error> = TaskResult.error x + let inline apply fTRO xTRO = map2 (fun f x -> f x) fTRO xTRO /// Replaces the wrapped value with unit