Orx.Fun.Option
1.3.0
dotnet add package Orx.Fun.Option --version 1.3.0
NuGet\Install-Package Orx.Fun.Option -Version 1.3.0
<PackageReference Include="Orx.Fun.Option" Version="1.3.0" />
paket add Orx.Fun.Option --version 1.3.0
#r "nuget: Orx.Fun.Option, 1.3.0"
// Install Orx.Fun.Option as a Cake Addin #addin nuget:?package=Orx.Fun.Option&version=1.3.0 // Install Orx.Fun.Option as a Cake Tool #tool nuget:?package=Orx.Fun.Option&version=1.3.0
Orx.Fun.Option
An option type for C# aiming to be explicit while concise.
Complete auto-generated documentation can be found here: sandcastle-documentation.
Why?
- Optional (maybe) values are necessary.
- Explicit optional arguments
- Explicit optional returns
- Continuations without overwhelming null/none checks
- ...
- Built-in nullables do not seem to satisfy the requirements.
Explicit Optional Arguments
static void Solve(Instance instance, Opt<SolverParameters> parameters = default)
{
// solve with the parameters if provided;
// with default parameters otherwise.
}
// caller knows that it is okay not to provide parameters
Solve(instance);
Solve(instance, None<SolverParameters>());
// or solve with parameters
SolverParameters pars = new()
{
TimeLimit = 60,
};
Solve(instance, Some(pars));
Explicit Optional Returns
An easy example to demonstrate this is the linq method First:
var numbers = new int[] { 1, 3, 5, 7 };
int firstEven = numbers.First(x => x % 2 == 0); // throws!!!
First method actually does not (cannot always) return an int. FirstOrDefault certainly does not fix the issue. On the other hand, solution is easy with optionals:
var numbers = new int[] { 1, 3, 5, 7 };
Opt<int> firstEven = numbers.FirstOrNone(x => x % 2 == 0);
Continuations
The following dummy example illustrates the use of Map and FlatMap to enable continuations.
/// Returns Some of the user if there exists one with the given id; None otherwise
static Opt<User> GetUserById(int id)
{
// ...
}
// task to achieve:
// 1. get the first even number
// 2. halve it
// 3. get the user with the number as id, if exists
// 4. return the users's name
var numbers = new int[] { 1, 3, 5, 7 };
Opt<string> name = numbers.FirstOrNone(x => x % 2 == 0)
.Map(x => x / 2)
.FlatMap(GetUserById)
.Map(user => user.Name);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Orx.Fun.Option:
Package | Downloads |
---|---|
Orx.Fun.Result
A simple result type for C#, not minding the error type, instead aiming to be explicit, concise and fluent. |
|
Orx.MathProg
A mathematical modeling library that enables building, documenting and solving concise, simple and reusable models. |
|
Orx.Fun.Compose
A function composition library for C#, heavily experimental right now, would be more practical if C# gets generic operator overloading. |
|
Orx.Fun.FunVec
Unified functional collections for C# allowing access by index. |
GitHub repositories
This package is not used by any popular GitHub repositories.