FunctionJunction 0.5.0-beta
dotnet add package FunctionJunction --version 0.5.0-beta
NuGet\Install-Package FunctionJunction -Version 0.5.0-beta
<PackageReference Include="FunctionJunction" Version="0.5.0-beta" />
<PackageVersion Include="FunctionJunction" Version="0.5.0-beta" />
<PackageReference Include="FunctionJunction" />
paket add FunctionJunction --version 0.5.0-beta
#r "nuget: FunctionJunction, 0.5.0-beta"
#:package FunctionJunction@0.5.0-beta
#addin nuget:?package=FunctionJunction&version=0.5.0-beta&prerelease
#tool nuget:?package=FunctionJunction&version=0.5.0-beta&prerelease
FunctionJunction
A functional programming library for C# that provides Option and Result types, discriminated unions, and functional combinators with comprehensive async support.
Installation
dotnet add package FunctionJunction
Core Types
Option<T>
Represents a value that may or may not exist.
using FunctionJunction;
using static FunctionJunction.Prelude;
// Create options
var some = Some(42);
var none = None<int>();
// Transform values
var doubled = some.Map(x => x * 2);
var parsed = "123".TryParse(out int n) ? Some(n) : None<int>();
// Chain operations
var result = LoadConfig()
.FlatMap(config => config.ConnectionString)
.Filter(conn => !string.IsNullOrEmpty(conn))
.UnwrapOr(() => "DefaultConnection");
Result<TOk, TError>
Represents an operation that can succeed with TOk or fail with TError.
// Create results
Result<int, string> success = 42; // Implicit conversion
Result<int, string> failure = "Error occurred";
// Transform and validate
var result = ParseInt(userInput)
.Map(x => x * 2)
.Validate(x => x > 0, _ => "Value must be positive")
.MapError(error => $"Validation failed: {error}");
// Error recovery
var recovered = result
.Recover(TryAlternativeMethod)
.UnwrapOr(error => DefaultValue);
Discriminated Unions
Create sum types with automatic pattern matching via source generation.
[DiscriminatedUnion]
public partial record PaymentResult
{
public record Success(string TransactionId, decimal Amount) : PaymentResult;
public record Declined(string Reason) : PaymentResult;
public record Error(Exception Exception) : PaymentResult;
}
// Generated Match method
var message = paymentResult.Match(
onSuccess: (id, amount) => $"Payment ${amount} succeeded: {id}",
onDeclined: reason => $"Payment declined: {reason}",
onError: ex => $"Payment failed: {ex.Message}"
);
Async Support
All operations have async counterparts that work with Task-returning functions with Await
prefix:
// Async operations
var userData = await userIdOption
.FlatMap(FetchUserAsync)
.AwaitFilter(user => user.IsActive)
.AwaitMap(EnrichUserData)
.AwaitUnwrapOr(GetDefaultUser);
// Combining multiple async operations
var result = await Result.All(
() => ValidateEmail(email),
() => CheckUserExists(email),
() => VerifyNotBlacklisted(email)
);
// Async enumerable extensions
await productsIds
.ToAsyncEnumerable()
.SelectWhere(async id => await TryLoadProduct(id))
.Scan(0m, (total, product) => total + product.Price)
.Last();
API Reference
Option<T> Methods
Map<TResult>(Func<T, TResult>)
- Transform the value if presentFlatMap<TResult>(Func<T, Option<TResult>>)
- Chain operations that return OptionsFilter(Func<T, bool>)
- Keep value only if predicate returns trueOr(Func<Option<T>>)
- Provide alternative if NoneAnd<TOther>(Func<Option<TOther>>)
- Combine two Options into tupleUnwrapOr(Func<T>)
- Extract value or provide defaultUnwrapOrThrow<TException>(Func<TException>)
- Extract value or throwTryUnwrap(out T?)
- Try pattern for safe extraction
Result<TOk, TError> Methods
Map<TResult>(Func<TOk, TResult>)
- Transform success valueMapError<TResult>(Func<TError, TResult>)
- Transform error valueFlatMap<TResult>(Func<TOk, Result<TResult, TError>>)
- Chain operationsRecover<TResult>(Func<TError, Result<TOk, TResult>>)
- Attempt error recoveryValidate(Func<TOk, bool>, Func<TOk, TError>)
- Add validationAnd<TOther>(Func<Result<TOther, TError>>)
- Combine if both succeedOr<TOther>(Func<Result<TOk, TOther>>)
- Try alternative on errorSwap()
- Exchange success and error positionsTryUnwrap(out TOk?)
/TryUnwrapError(out TError?)
- Try patterns
Static Helpers
Option.Some<T>(T)
/Option.None<T>()
- Create OptionsResult.Ok<TOk, TError>(TOk)
/Result.Error<TOk, TError>(TError)
- Create ResultsOption.All(IEnumerable<Option<T>>)
- Combine Options (all must be Some)Option.Any(IEnumerable<Option<T>>)
- Find first SomeResult.All(IEnumerable<Result<TOk, TError>>)
- Combine Results (all must succeed)Result.Any(IEnumerable<Result<TOk, TError>>)
- Find first successTry.Execute<TOk>(Func<TOk>)
- Convert exceptions to ResultsTry<TException>.Execute<TOk>(Func<TOk>)
- Catch specific exception types
Iterator Extensions
Enumerate<T>()
- Pair elements with indicesScan<TSource, TResult>()
- Running accumulation with intermediatesSelectWhere<TSource, TResult>()
- Combined Select+Where using OptionTakeWhileInclusive<T>()
- Take while true, including first false
Source Generators
DiscriminatedUnion Attribute
Configure discriminated union generation:
[DiscriminatedUnion(
MatchOn = MatchUnionOn.Deconstruct, // or Type, None
GeneratePolymorphicSerialization = true,
GeneratePrivateConstructor = true
)]
public partial record Command { /* ... */ }
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 is compatible. 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 is compatible. 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. |
.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. |
-
.NETStandard 2.0
- Microsoft.Bcl.AsyncInterfaces (>= 8.0.0)
- System.Collections.Immutable (>= 9.0.0)
-
net8.0
- No dependencies.
-
net9.0
- 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 |
---|---|---|
0.5.0-beta | 53 | 7/11/2025 |
0.4.0-beta | 111 | 7/9/2025 |
0.3.0-alpha | 113 | 7/8/2025 |
0.2.0-alpha | 103 | 6/29/2025 |
0.1.0-alpha | 40 | 6/28/2025 |