Swallow.Functional
0.0.1
dotnet add package Swallow.Functional --version 0.0.1
NuGet\Install-Package Swallow.Functional -Version 0.0.1
<PackageReference Include="Swallow.Functional" Version="0.0.1" />
paket add Swallow.Functional --version 0.0.1
#r "nuget: Swallow.Functional, 0.0.1"
// Install Swallow.Functional as a Cake Addin
#addin nuget:?package=Swallow.Functional&version=0.0.1
// Install Swallow.Functional as a Cake Tool
#tool nuget:?package=Swallow.Functional&version=0.0.1
Swallow.Functional
A small glimpse of functional programming for C#. This includes implementations of the Maybe
and
Either
functors, as well as some helpers and extensions regarding functions.
This library is heavily influenced by Haskell. If you know your way around haskell, you might see lots of familiar stuff.
Maybe
Maybe
is a type that can either contain a value wrapped as Just
or be Nothing
, i.e. a safer
null.
This is a way to work around the
Billion Dollar Mistake.
Once you have a Maybe
, either via explicit static functions on the type or via the ToMaybe
extension, you can act on the maybe with Then
, Bind
and Map
, modifying the Just
but
leaving the Nothing
untouched.
Unwrapping is allowed but should not be used primarily; by using FromJust
, you can return the
Just
-value or either return a fallback or throw an exception.
Example
var result = SomeCallThatCanReturnNull()
.ToMaybe()
.Map(x => SomeFunction(x))
.Bind(x => SomeFunctionThatReturnsMaybe(x));
Either
Either
is similar to maybe, but instead of having just one yes-or-no, you have two possible values. An
Either
can either be Left
- which is generally used in case of errors - or a Right
- which is
generally used as success.
The usage is very similar to Maybe
with Map
, Bind
and Then
, but you also have MapLeft
to
map the Left
value. This is needed, because Bind
and Then
require the result's left to be
the same as the left of the either that is acted on. You can either create an Either
explicitly
or via the extension ToEither
, which is very similar to ToMaybe
.
The same rule for unwrapping applies as for Maybe
, but there is a generally safer version, too:
FromEither
takes in two functions to coalesce both left and right to one type, resulting in a
safe operation.
Example
var result = SomeCallThatCanReturnNull()
.ToEither<string, int>()
.Map(x => SomeFunction(x))
.Bind(x => SomeFunctionThatReturnsMaybe(x))
.FromEither(_ => 0, right => right);
Function extensions
There are a number of function extensions to promote a more functional style. These include but are not limited to:
- Currying/Uncurrying: Turn functions that take two parameters into functions that take tuples and vice versa
- Compose: Chain two functions together
- Flip: Flip the arguments of a function
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. |
This package has 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.
Version | Downloads | Last updated |
---|