Kj.Functional.Lib
0.2.2
See the version list below for details.
dotnet add package Kj.Functional.Lib --version 0.2.2
NuGet\Install-Package Kj.Functional.Lib -Version 0.2.2
<PackageReference Include="Kj.Functional.Lib" Version="0.2.2" />
<PackageVersion Include="Kj.Functional.Lib" Version="0.2.2" />
<PackageReference Include="Kj.Functional.Lib" />
paket add Kj.Functional.Lib --version 0.2.2
#r "nuget: Kj.Functional.Lib, 0.2.2"
#:package Kj.Functional.Lib@0.2.2
#addin nuget:?package=Kj.Functional.Lib&version=0.2.2
#tool nuget:?package=Kj.Functional.Lib&version=0.2.2
Description
A simple library of structures and extension to enable writing c# code in a more functional style. Avoid having to check for null values with usage of Option and Either.
Most importand structures
Two most important structures to use are Option and Either.
Option represents something that may (but does not have to) exist. An Option can be one of two: Some or None. Examples would be:
- parsing result (valid parsed value or nothing)
- search result (valid record found or nothing)
Either represents one of two possible result, 'left' or 'right'. 'Left' can mostly be used to represent a valid result, the 'right' to represent an error that occured.
Code examples
How to use an Option:
string inputString = "123";
// parsedOptional contains either a value (123) or nothing, depending on the result of TryParse
// in this case it's valid value
var parsedOptional = inputString.TryParseInt();
// if parsedOptional contains a value AND if this value satisfies the condition (>100)
// then filtered becomes an optional with this value. Otherwise it gets to None.
var filtered = parsedOptional.Where(i => i > 100);
// we can use do to utilize to value without a result, i.e. for example to log.
filtered
.Do(v => Console.WriteLine("Valid value"),
() => Console.WriteLine("Value missing"));
// at the end ot the processing, let's map the optional value into a something final
var finalValue = filtered
.Match(v => $"correct value of {v}",
() => "Incorrect value");`
How to use an Either:
string stringToParse = "123";
// ParseString return Either<int, string> (depending if successful or not)
var parseResult = ParseString(stringToParse);
var finalResult = parseResult
// we can (although we don't have to) utilize the value contained in Either for example to log
.Do(
lv=>Console.WriteLine($"Correct value of {lv}"),
rv=>Console.WriteLine($"error: {rv}")
)
//
.Match(
i => $"Length of {i}",
errorText => $"Parse error: {errorText}");
Example of chained calls using Either:
Function calls returning Either can be chained, making the code more readable and logically structured. Important: as long the the methods that are being called consecutively stick to the same signature they can be chained as in this example.
string inputString = "ABC";
// Obtaining the result from the combined (chained) calls to consecutive methods.
// Each method gets called only if the preceeding one succeeds.
var combinedResult = await ParseAsync(inputString)
.BindResultAsync(ValidateAsync)
.BindResultAsync(ModifyUserAsync);
// Now can use the result after the chained calls:
var finalResult = combinedResult
.Match(user => $"Processed user: {user.FirstName}",
err => $"Error: {err.ErrorText}");
Any next function is called only if the previous one returns the Either with the 'left hand' part (valid result). Otherwise the further methods do not get called and processing stops with the 'right hand' value (error object).
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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Kj.Functional.Lib:
Package | Downloads |
---|---|
Kj.Functional.Lib.Extensions
Small functional extensions library for C# |
GitHub repositories
This package is not used by any popular GitHub repositories.