From 2efcd92094ed54fb5966c3a8f4aad313669c2c45 Mon Sep 17 00:00:00 2001 From: Jack Edwards Date: Thu, 3 Apr 2025 09:03:14 -0500 Subject: [PATCH] Add overload for Either.Match that accepts a Func for neither --- EasyMonads/Either/Either.cs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/EasyMonads/Either/Either.cs b/EasyMonads/Either/Either.cs index 83cdab5..1729f95 100644 --- a/EasyMonads/Either/Either.cs +++ b/EasyMonads/Either/Either.cs @@ -159,7 +159,7 @@ private static void ValidateFunction(Func function) throw new ArgumentNullException(nameof(function)); } } - + private static void ValidateMatch(Func left, Func right) { if (left is null) @@ -173,6 +173,16 @@ private static void ValidateMatch(Func left, Func } } + private static void ValidateMatch(Func neither, Func left, Func right) + { + if (neither is null) + { + throw new ArgumentNullException(nameof(neither)); + } + + ValidateMatch(left, right); + } + public TResult Match(Func left, Func right, TResult neither) { ValidateMatch(left, right); @@ -187,6 +197,20 @@ public TResult Match(Func left, Func r #pragma warning restore CS8524 } + public TResult Match(Func left, Func right, Func neither) + { + ValidateMatch(neither, left, right); + +#pragma warning disable CS8524 + return _state switch + { + EitherState.Neither => neither(), + EitherState.Left => left(_left!), + EitherState.Right => right(_right!) + }; +#pragma warning restore CS8524 + } + public TResult Match(TResult leftOrNeither, Func right) { ValidateFunction(right);