Pure-Monads
1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Pure-Monads --version 1.0.2
NuGet\Install-Package Pure-Monads -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pure-Monads" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pure-Monads --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Pure-Monads, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Pure-Monads as a Cake Addin #addin nuget:?package=Pure-Monads&version=1.0.2 // Install Pure-Monads as a Cake Tool #tool nuget:?package=Pure-Monads&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Pure Monads
Contains implementations of the most commonly used monads:
- Option
- Result
Also contains Pipe extensions for chaining function calls.
Examples
Option
using static Option;
// Option instances can be created:
var some1 = Option<int>.Some(1); // via factory method
var some2 = 2.Some(); // via extension method
Option<int> some3 = 3; // by implicit conversion
var none1 = Option<int>.None(); // via factory method
var none2 = None<int>(); // via static method in non-generic Option class
// Standard monad operations:
var mapResult = some1.Map(value => value + 10); // mapResult is Some(11)
var flatMapResult = some2.FlatMap(value => (value + 10).Some()); // flatMapResult is Some(12)
// Matching can be done:
var matchResult = none1.Match(value => $"Some {value}!", () => "None!"); // matchResult is "None!"
// Making nullable value into Option instance:
var notNull = "I'm not null!";
var someFromNotNull = notNull.NullAsNone(); // Now it's Some("I'm not null!")
string? itIsNull = null;
var noneFromNull = itIsNull.NullAsNone(); // Now it's None
// Providing alternative values for None:
int alt1 = none1.Or(100); // Directly passed alternative value
int alt2 = none1.Or(() => 100); // Alternative value retrieved from delegate
Option<int> alt3 = none1.Or(100.Some()); // Directly passed alternative monad
Option<int> alt4 = none1.Or(() => 100.Some()); // Alternative monad retrieved from delegate
// Extracting a value from Some or throwing Exception for None:
try
{
none1.ValueOrFailure(); // Throws exception as none1 is None
}
catch {}
// Returning a Some value or None from dictionary:
var dict = new Dictionary<int, string>
{
{ 1, "One" },
{ 2, "Two" },
};
var dictValue1 = dict.GetOrNone(1); // dictValue1 is Some("One")
var dictValue3 = dict.GetOrNone(3); // dictValue3 is None
Result
The following code samples are for Result<TValue, TError> class.
A version with TError defaulted to Exception exists: Result<TValue>. This version has identical methods except only single generic type parameter is used.
using static Result;
// Result instances can be created:
var value1 = Result<int, string>.Value(1); // via factory method
var value2 = Value<int, string>(2); // via factory method from non-generic Result class
Result<int, string> value3 = 3; // by implicit conversion
var error1 = Result<int, string>.Error("Error 1"); // via factory method
var error2 = Error<int, string>("Error 2"); // via factory method from non-generic Result class
Result<int, string> error3 = "Error 3"; // by implicit conversion
// Standard monad operations:
var mapResult = value1.Map(value => value + 10); // mapResult is Value(11)
var flatMapResult = value2.FlatMap(value => Value<int, string>(value + 10)); // flatMapResult is Value(12)
// Matching can be done:
var matchResult = error1.Match( // matchResult is "Error: Error 1"
value => $"Value: {value}",
err => $"Error: {err}");
// Extracting value or error as Option<TValue> or Option<TError>:
var someValue1 = value1.Value(); // someValue1 is Some(1)
var noneError1 = value1.Error(); // noneError1 is None
var noneValue2 = error2.Value(); // noneValue2 is None
var someError2 = error2.Error(); // someError2 is Some("Error 2")
// Result can be used instead of try-catch:
var result1 = Result.From(() => "Value 1"); // result1 is Value("Value 1)"
var result2 = Result.From(() => // result2 is Error(Exception)
{
throw new Exception("Error 1");
return "Value 1";
});
// Async versions of Result as try-catch also exist:
var result3 = await Result.FromAsync(async () => // result3 is Value("Value 1)"
{
await Task.CompletedTask;
return "Value 1";
});
var result4 = await Result.FromAsync(async () => // result4 is Error(Exception)
{
await Task.CompletedTask;
throw new Exception("Error 1");
return "Value 1";
});
Pipe
// Used functions
int Square(int x) => x * x;
int Add5(int x) => x + 5;
async Task<int> DoubleAsync(int x) => await Task.FromResult(x * 2);
async Task<int> Subtract2Async(int x) => await Task.FromResult(x - 2);
// Chaining functions application:
var result1 = 10 // result1 is 55
.Pipe(Square)
.Pipe(x => x / 2)
.Pipe(Add5);
// Async functions application chaining:
var result2 = await 3 // result2 is 64
.PipeAsync(DoubleAsync)
.PipeAsync(Subtract2Async)
.PipeAsync(async x => await Task.FromResult(x * x * x));
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.