From c742aa86d29fe86ac0b69bd1ee7d9c561b660489 Mon Sep 17 00:00:00 2001 From: tima Date: Fri, 1 Feb 2019 10:23:44 +0300 Subject: [PATCH 1/3] Refactor/Rename --- Functional.Maybe/Either.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Functional.Maybe/Either.cs b/Functional.Maybe/Either.cs index 2d875e0..34cd974 100644 --- a/Functional.Maybe/Either.cs +++ b/Functional.Maybe/Either.cs @@ -66,19 +66,19 @@ public T Match(Func resultFunc, Func errorFunc) /// /// Executes result or error function depending on the Either state. /// - public T Match(Func leftFunc, Func rightFunc) + public T Match(Func resultFunc, Func errorFunc) { - if (leftFunc == null) + if (resultFunc == null) { - throw new ArgumentNullException(nameof(leftFunc)); + throw new ArgumentNullException(nameof(resultFunc)); } - if (rightFunc == null) + if (errorFunc == null) { - throw new ArgumentNullException(nameof(rightFunc)); + throw new ArgumentNullException(nameof(errorFunc)); } - return _success ? leftFunc() : rightFunc(); + return _success ? resultFunc() : errorFunc(); } /// From d2015c62a4261f9b4dc98dbccd272477ec6a7477 Mon Sep 17 00:00:00 2001 From: tima Date: Fri, 1 Feb 2019 17:46:18 +0300 Subject: [PATCH 2/3] Simplified constructors --- Functional.Maybe/Either.cs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/Functional.Maybe/Either.cs b/Functional.Maybe/Either.cs index 34cd974..aedc8dd 100644 --- a/Functional.Maybe/Either.cs +++ b/Functional.Maybe/Either.cs @@ -13,20 +13,18 @@ public readonly struct Either private readonly TError _errorValue; private readonly bool _success; - private Either(TResult result, TError error, bool success) + private Either(TResult result) { - _success = success; + _success = true; + _errorValue = default; + _resultValue = result; + } - if (success) - { - _resultValue = result; - _errorValue = default; - } - else - { - _errorValue = error; - _resultValue = default; - } + private Either(TError error) + { + _success = false; + _errorValue = error; + _resultValue = default; } /// @@ -34,7 +32,7 @@ private Either(TResult result, TError error, bool success) /// public static Either Result(TResult result) { - return new Either(result, default, true); + return new Either(result); } /// @@ -42,7 +40,7 @@ public static Either Result(TResult result) /// public static Either Error(TError error) { - return new Either(default, error, false); + return new Either(error); } /// From 240ba0084f8996bc0a661cf7a8c9176fdde3d5d2 Mon Sep 17 00:00:00 2001 From: tima Date: Sat, 9 Feb 2019 16:29:18 +0300 Subject: [PATCH 3/3] Made Success property public for reading --- Functional.Maybe.Tests/EitherTests.cs | 4 +++- Functional.Maybe/Either.cs | 15 ++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Functional.Maybe.Tests/EitherTests.cs b/Functional.Maybe.Tests/EitherTests.cs index 0f352fa..ef154ec 100644 --- a/Functional.Maybe.Tests/EitherTests.cs +++ b/Functional.Maybe.Tests/EitherTests.cs @@ -159,9 +159,11 @@ public void ExtensionMethodTests() Assert.AreEqual(29, eitherResult.ResultOrDefault()); Assert.AreEqual(null, eitherResult.ErrorOrDefault()); + Assert.IsTrue(eitherResult.Success); Assert.AreEqual("Twenty nine", eitherError.ErrorOrDefault()); - Assert.AreEqual(0, eitherError.ResultOrDefault()); + Assert.AreEqual(default, eitherError.ResultOrDefault()); + Assert.IsFalse(eitherError.Success); } } diff --git a/Functional.Maybe/Either.cs b/Functional.Maybe/Either.cs index aedc8dd..6b39d5c 100644 --- a/Functional.Maybe/Either.cs +++ b/Functional.Maybe/Either.cs @@ -11,18 +11,19 @@ public readonly struct Either { private readonly TResult _resultValue; private readonly TError _errorValue; - private readonly bool _success; + + public bool Success { get; } private Either(TResult result) { - _success = true; + Success = true; _errorValue = default; _resultValue = result; } private Either(TError error) { - _success = false; + Success = false; _errorValue = error; _resultValue = default; } @@ -58,7 +59,7 @@ public T Match(Func resultFunc, Func errorFunc) throw new ArgumentNullException(nameof(errorFunc)); } - return _success ? resultFunc(_resultValue) : errorFunc(_errorValue); + return Success ? resultFunc(_resultValue) : errorFunc(_errorValue); } /// @@ -76,7 +77,7 @@ public T Match(Func resultFunc, Func errorFunc) throw new ArgumentNullException(nameof(errorFunc)); } - return _success ? resultFunc() : errorFunc(); + return Success ? resultFunc() : errorFunc(); } /// @@ -94,7 +95,7 @@ public void Match(Action resultAction, Action errorAction) throw new ArgumentNullException(nameof(errorAction)); } - if (_success) + if (Success) { resultAction(_resultValue); } @@ -119,7 +120,7 @@ public void Match(Action resultAction, Action errorAction) throw new ArgumentNullException(nameof(errorAction)); } - if (_success) + if (Success) { resultAction(); }