Bearded.Monads
1.4.45
See the version list below for details.
dotnet add package Bearded.Monads --version 1.4.45
NuGet\Install-Package Bearded.Monads -Version 1.4.45
<PackageReference Include="Bearded.Monads" Version="1.4.45" />
paket add Bearded.Monads --version 1.4.45
#r "nuget: Bearded.Monads, 1.4.45"
// Install Bearded.Monads as a Cake Addin #addin nuget:?package=Bearded.Monads&version=1.4.45 // Install Bearded.Monads as a Cake Tool #tool nuget:?package=Bearded.Monads&version=1.4.45
Bearded.Monads
Monads for use in C#. These include implementations of SelectMany
(aka. bind
) so you can use C#s fluent linq syntax.
Currently provides Option and Either, as they are useful for error checking, as well as task.
Also provides applicative instances for Task and Maybe.
Installation
Bearded.Monads is available from NuGet:
Install-Package Bearded.Monads
Then, just add using Bearded.Monads;
to the top of your C# source file. There is also a Bearded.Monads.Syntax
module that you can reference using using static Bearded.Monads.Syntax;
with your other using statements.
Option
An obvious implementation which provides various helper methods. This allows you to avoid passing back null
from methods and perfoming some special logic. By using Option
, you enable the compiler to check that you've handled the missing case.
Usage
As an academic example, consider a trivial method that might have looked thusly without Option:
public bool TryParseInt(string input, out int output)
{
if(int.TryParse(input, out int i))
{
output = i;
return true;
}
output = default
return false;
}
Can now be transformed into something a little more sane:
public Option<int> TryParseInt(string input)
{
if(int.TryParse(input, out int i))
{
// Note the implicit operator that converts the `A` to an `Option<A>`
return i;
}
return Option<int>.None;
}
As a more realistic example, imagine loading something from a database:
public MyEntity LoadEntityBy(int id)
{
// Assume Dapper is being used...
using(var connection = new IDbConnection(_connectionString))
{
string sql = "SELECT * FROM Entity WHERE Id = @Id";
var result = connection.Query<DbEntity>(sql, new { Id = id });
if(result.Any())
{
return _mapper.From(result.First()).To(new MyEntity());
}
return null;
}
}
Becomes:
public Option<MyEntity> LoadEntityBy(int id)
{
// Assume Dapper is being used...
using(var connection = new IDbConnection(_connectionString))
{
string sql = "SELECT * FROM Entity WHERE Id = @Id";
var result = connection.Query<DbEntity>(sql, new { Id = id });
if(result.Any())
{
return _mapper.From(result.First()).To(new MyEntity());
}
return Option<MyEntity>.None;
}
}
This doesn't seem to add much, but if you compose them:
public void LoadEntity(string fromPossibleId)
{
var maybeEntity = from id in TryParseInt(fromPossibleId)
from entity in LoadEntityBy(id)
select entity;
// This will shortcircuit if none of these work.
}
Either
This has been renamed from EitherSuccessOrFailure
, and takes it's "success" value as the first type parameter.
This was done as Either "short circuits" on a Left
in haskell, but this seems a little unnatural from C#. Please raise an issue if you don't believe this is the case.
Usage
This is useful to return some error condition from a long chain of Either results (or even via the AsEither
when dealing with Option results).
For example, if I have the following chain of optional results:
public Option<ResultFromTertiaryService> LoadFromAMultitudeOfServices(string value)
{
return from id in TryParseInt(value)
from first in ExpensiveCallToAWebServiceThatMightFail(id)
from second in TryAndLoadARecordFromTheDatabase(id, first.ClientData.SomeField)
from third in TryAndFindDataInTertiaryService(id, second.AnotherField, first.Some.Other.Context)
select third;
}
This might fail at any point, so it's helpful to tag the None
with some helpful context by using AsEither
to convert from Option<Success>
to Either<Success, string>
e.g.
public Either<ResultFromTertiaryService,string> LoadFromAMultitudeOfServices(string value)
{
return from id in TryParseInt(value).AsEither("Failed to parse ({0}) into an id", value)
from first in ExpensiveCallToAWebServiceThatMightFail(id).AsEither("Didn't find a value")
from second in TryAndLoadARecordFromTheDatabase(id, first.ClientData.SomeField).AsEither("Couldn't find {0} in the database", id)
from third in TryAndFindDataInTertiaryService(id, second.AnotherField, first.Some.Other.Context).AsEither("Failed to load from tertiary source")
select third;
}
Try
This is similar to Either, but uses an exception in the error case. This is useful for things like the SafeCallback
extension method over all objects, which provides an ergonomic version of wrapping everything in a try-catch block.
Task Applicative (aka Asynquence)
This is probable the most interesting use of Task
. This allows one to chain together a sequence of tasks and provide a callback at the end to produce a final result.
It's recommended to use the below to bring the class into scope directly.
using static Bearded.Monads.Syntax;
Then usage is as follows:
var result = await Asynquence(Task.FromResult(10))
.And(Task.FromResult(10))
.And(Task.FromResult(10))
.And(Task.FromResult(10))
.Select((a, b, c, d) => a + b + c + d);
var expected = 40;
Assert.Equal(expected, result);
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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Bearded.Monads:
Package | Downloads |
---|---|
Csn.Trade.Utils
Package Description |
|
Csn.Trade.ApiUtils
Package Description |
|
confudge
Confudge makes it easy to use typed configuration without the overhead of creating adapter objects. It uses a convention and some overridable defaults to quickly enable typed configuration in .net. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.4.47 | 101 | 10/1/2024 | |
1.4.46 | 100 | 10/1/2024 | |
1.4.45 | 4,163 | 1/3/2024 | |
1.4.43 | 386 | 1/3/2024 | |
1.4.42 | 715 | 12/3/2023 | |
1.4.40 | 4,790 | 2/3/2023 | |
1.4.39 | 826 | 2/3/2023 | |
1.4.1 | 849 | 2/3/2023 | |
1.3.76 | 38,833 | 12/20/2018 | |
1.3.75 | 1,346 | 12/20/2018 | |
1.3.74 | 1,297 | 12/20/2018 | |
1.3.73 | 1,396 | 11/26/2018 | |
1.3.72 | 1,738 | 11/13/2018 | |
1.3.69 | 1,378 | 10/29/2018 | |
1.3.67 | 1,374 | 10/26/2018 | |
1.3.60 | 24,143 | 4/23/2018 | |
1.3.55 | 4,666 | 2/27/2018 | |
1.3.48 | 4,387 | 11/14/2017 | |
1.3.46 | 1,598 | 9/20/2017 | |
1.3.45 | 1,496 | 9/14/2017 | |
1.3.44 | 1,523 | 9/13/2017 | |
1.3.43 | 1,555 | 9/12/2017 | |
1.3.41 | 1,521 | 9/12/2017 | |
1.3.40 | 1,544 | 9/6/2017 | |
1.3.37 | 4,887 | 9/6/2017 | |
1.3.36 | 1,539 | 9/6/2017 | |
1.3.35 | 1,568 | 9/6/2017 | |
1.3.34 | 1,532 | 9/6/2017 | |
1.3.31 | 1,561 | 9/5/2017 | |
1.3.30 | 1,558 | 9/5/2017 | |
1.3.28 | 1,549 | 9/5/2017 | |
1.3.27 | 4,597 | 8/23/2017 | |
1.3.26 | 8,664 | 8/21/2017 | |
1.3.25 | 10,947 | 8/16/2017 | |
1.3.23 | 1,554 | 8/14/2017 | |
1.3.21 | 1,536 | 8/3/2017 | |
1.3.20 | 1,625 | 8/3/2017 | |
1.3.18 | 1,504 | 8/3/2017 | |
1.3.16 | 1,543 | 8/2/2017 | |
1.3.15 | 1,747 | 8/1/2017 | |
1.3.14 | 1,571 | 7/28/2017 | |
1.3.13 | 1,622 | 7/6/2017 | |
1.3.12 | 1,682 | 6/22/2017 | |
1.3.11 | 1,635 | 6/22/2017 | |
1.3.10 | 1,581 | 6/22/2017 | |
1.3.9 | 1,626 | 5/25/2017 | |
1.3.8 | 1,645 | 5/24/2017 | |
1.2.2 | 1,602 | 5/24/2017 | |
1.2.1 | 1,680 | 4/26/2017 | |
1.1.5 | 5,751 | 8/18/2016 | |
1.1.4 | 1,902 | 7/12/2016 | |
1.1.3 | 2,108 | 6/12/2016 | |
1.1.2 | 2,362 | 5/31/2016 | |
1.1.1 | 1,791 | 5/9/2016 | |
1.1.0 | 1,730 | 2/15/2016 | |
1.0.25 | 1,885 | 10/29/2015 | |
1.0.24 | 1,812 | 9/25/2015 | |
1.0.23 | 1,783 | 9/24/2015 | |
1.0.22 | 1,768 | 9/16/2015 | |
1.0.21 | 1,781 | 9/4/2015 | |
1.0.20 | 1,920 | 6/26/2015 | |
1.0.19 | 1,842 | 6/24/2015 | |
1.0.18 | 1,732 | 6/3/2015 | |
1.0.17 | 1,979 | 6/3/2015 | |
1.0.16 | 1,759 | 5/6/2015 | |
1.0.15 | 2,622 | 11/30/2014 | |
1.0.14 | 1,876 | 11/25/2014 | |
1.0.13 | 1,815 | 10/22/2014 | |
1.0.12 | 1,969 | 10/7/2014 | |
1.0.11 | 1,875 | 10/7/2014 | |
1.0.10 | 2,155 | 8/3/2014 | |
1.0.9 | 1,837 | 8/3/2014 | |
1.0.8 | 1,756 | 8/3/2014 | |
1.0.7 | 1,952 | 7/30/2014 | |
1.0.6 | 1,936 | 6/25/2014 | |
1.0.5 | 1,844 | 6/25/2014 | |
1.0.4 | 1,860 | 6/16/2014 | |
1.0.3 | 2,159 | 3/24/2014 | |
1.0.2 | 1,936 | 3/19/2014 | |
1.0.0 | 1,803 | 3/19/2014 |