FluentUtils.Monad
3.0.0
dotnet add package FluentUtils.Monad --version 3.0.0
NuGet\Install-Package FluentUtils.Monad -Version 3.0.0
<PackageReference Include="FluentUtils.Monad" Version="3.0.0" />
<PackageVersion Include="FluentUtils.Monad" Version="3.0.0" />
<PackageReference Include="FluentUtils.Monad" />
paket add FluentUtils.Monad --version 3.0.0
#r "nuget: FluentUtils.Monad, 3.0.0"
#:package FluentUtils.Monad@3.0.0
#addin nuget:?package=FluentUtils.Monad&version=3.0.0
#tool nuget:?package=FluentUtils.Monad&version=3.0.0
FluentUtils.Monad
This library contains an implementation of the Result monad.
Basic Usage
var okResult = Result.Ok("bob");
// access value of result
okResult.Match(
value => Console.WriteLine(value), // "bob"
err => Console.WriteLine(err.ToString()) // skipped
);
var errResult = Result.Error(new Error(new ErrorCode("1"), new ErrorMessage("No bob")));
errResult.Match(
value => Console.WriteLine(value), // skipped
err => Console.WriteLine(err.ToString()) // "1: Not bob"
);
Async Usage
Order result = await CreateOrderAsync().MatchAsync(
async (order, token) => await UpdateOrderAsync(order, token),
async (err, token) => await ErrorOrderAsync(err, token),
cancellationToken
);
Concepts
Creating Results
Result.Ok
Creates the OkResultType<T>
from a value that you provide, or an Empty
value
if you do not provide one.
ResultType<string> result = Result.Ok("Hello World");
//^? OkResultType<string>
ResultType<Empty> result = Result.Ok();
//^? OkResultType<Empty>
Result.Error
Creates the ErrorResultType<T>
from an error that you provide.
ResultType<string> result = Result.Error<string>(new Error("Code", "Message"));
//^? ErrorResultType<string>
ResultType<Empty> result = Result.Error(new Error("Code", "Message"));
//^? ErrorResultType<Empty>
Bind
Binds a factory method into a result type by executing it inside a try/catch
block and converting the
result of the factory into an OkResultType<T>
if it is successful, or an
ErrorResultType<T>
if it throws an exception.
ResultType<string> okResult = Result.Bind(() => "hello world");
// ^? OkResultType<string>
ResultType<string> errorResult = Result.Bind(() => throw Exception());
// ^? ErrorResultType<string>
Accessing the Value inside a Result
Match
Checks the state of a ResultType<T>
, executing an okHandler
if it is an
OkResultType<T>
or an errorHandler
if it is an ErrorResultType<T>
.
See Basic Usage for an example
Unwrap
Extracts the value from a ResultType<T>
without checking if it is an
OkResultType<T>
or an ErrorResultType<T>
.
Unwrapping a ResultType<T>
will throw an UnwrapPanicException
if the
result is a ErrorResultType<T>
string value = Result.Ok("Hello World").Unwrap();
// ^? "Hello World"
Result.Error(new Error("Code", "Message")).Unwrap();
// ^? throws UnwrapPanicException
Transforming the Value inside a Result
Pipe
Transforms the value of a ResultType<T>
using the provided function if it is
an OkResultType<T>
,
otherwise propagates the error of the ErrorResultType<T>
.
bool stringIsGreaterThanFive = Result.Bind(() => "hello world")
.Pipe(value => value.Length)
.Pipe(length => length >= 5)
.Unwrap();
Executing Side Effects on the Value of a Result
Tap
Access the value of a result to execute a side effect. The result will be
converted into an ErrorResultType<T>
if the
side effect fails to execute due to an exception.
The side effect will only execute on an OkResultType<T>
Result.Ok("Hello World")
.Tap(value => Console.WriteLine(value));
Ensure
Ensures the value of a result passes the provided predicate. The result will be
converted into a ErrorResultType<T>
if the predicate does not return true
string result = Result.Ok("Hello World")
.Ensure(value => value.Length >= 1)
.Pipe(value => value.ToUpperCase())
.Unwrap(); // -> "HELLO WORLD"
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
3.0.0 | 122 | 10/1/2024 |
3.0.0-PullRequest0030.8 | 52 | 10/1/2024 |
3.0.0-PullRequest0030.7 | 55 | 9/30/2024 |
3.0.0-PullRequest0030.6 | 55 | 9/29/2024 |
2.0.0 | 122 | 9/26/2024 |
2.0.0-PullRequest0028.13 | 54 | 9/25/2024 |
1.3.0-PullRequest0028.12 | 50 | 9/25/2024 |
1.2.17 | 134 | 8/6/2024 |
1.2.16 | 169 | 7/21/2024 |
1.2.15 | 138 | 6/29/2024 |
1.2.14 | 126 | 6/19/2024 |
1.2.13 | 121 | 6/19/2024 |
1.2.12 | 130 | 5/7/2024 |
1.2.10 | 125 | 5/5/2024 |