Swallow.Functional 0.0.3

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Swallow.Functional --version 0.0.3
NuGet\Install-Package Swallow.Functional -Version 0.0.3
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="Swallow.Functional" Version="0.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Swallow.Functional --version 0.0.3
#r "nuget: Swallow.Functional, 0.0.3"
#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 Swallow.Functional as a Cake Addin
#addin nuget:?package=Swallow.Functional&version=0.0.3

// Install Swallow.Functional as a Cake Tool
#tool nuget:?package=Swallow.Functional&version=0.0.3

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);

Try

Try is again similar to Either, in that it is one of two possible values. Essentially, a Try encapsulates a throwing operation in a safe way, unmaking all progress in stack unwinding ever made.

The usage is the same as well - Map, Bind and Then. For convenience, Then and Bind also provide auto-lifting; they take in normal functions that throw exceptions and catch these.

Unwrapping is done via FromSuccess and FromException.

Example

var result = FunctionThatThrows.Catch("value")
    .Map(x => x * 2)
    .Bind(x => CallThatReturnsTry(x))
    .Then(() => FunctionThatThrows);

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 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.

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