diff --git a/src/FuncSharp.Tests/Collections/ExceptNullsTests.cs b/src/FuncSharp.Tests/Collections/ExceptNullsTests.cs index a2efaf7..c523ceb 100644 --- a/src/FuncSharp.Tests/Collections/ExceptNullsTests.cs +++ b/src/FuncSharp.Tests/Collections/ExceptNullsTests.cs @@ -3,7 +3,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class ExceptNullsTests { diff --git a/src/FuncSharp.Tests/Collections/ExceptTests.cs b/src/FuncSharp.Tests/Collections/ExceptTests.cs index 1394fb0..e69134a 100644 --- a/src/FuncSharp.Tests/Collections/ExceptTests.cs +++ b/src/FuncSharp.Tests/Collections/ExceptTests.cs @@ -2,7 +2,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class ExceptTests { diff --git a/src/FuncSharp.Tests/Collections/ExceptionsAggregateTests.cs b/src/FuncSharp.Tests/Collections/ExceptionsAggregateTests.cs index 6c615d6..cddcaac 100644 --- a/src/FuncSharp.Tests/Collections/ExceptionsAggregateTests.cs +++ b/src/FuncSharp.Tests/Collections/ExceptionsAggregateTests.cs @@ -3,12 +3,12 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class CustomException : Exception { public CustomException(string message) : base(message) { } - public bool Equals(CustomException other) => other is not null && other.Message.SafeEquals(Message); + public bool Equals(CustomException other) => Message.Equals(other?.Message); public override bool Equals(object obj) => Equals(obj as CustomException); public override int GetHashCode() => Message.GetHashCode(); } diff --git a/src/FuncSharp.Tests/Collections/FirstOptionTests.cs b/src/FuncSharp.Tests/Collections/FirstOptionTests.cs index 2b77138..16ce56e 100644 --- a/src/FuncSharp.Tests/Collections/FirstOptionTests.cs +++ b/src/FuncSharp.Tests/Collections/FirstOptionTests.cs @@ -2,7 +2,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class FirstOptionTests { diff --git a/src/FuncSharp.Tests/Collections/FlattenTests.cs b/src/FuncSharp.Tests/Collections/FlattenTests.cs index e2e4fcf..43066b9 100644 --- a/src/FuncSharp.Tests/Collections/FlattenTests.cs +++ b/src/FuncSharp.Tests/Collections/FlattenTests.cs @@ -2,7 +2,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class FlattenTests { diff --git a/src/FuncSharp.Tests/Collections/IsMultipleTests.cs b/src/FuncSharp.Tests/Collections/IsMultipleTests.cs index e16e325..4090ec4 100644 --- a/src/FuncSharp.Tests/Collections/IsMultipleTests.cs +++ b/src/FuncSharp.Tests/Collections/IsMultipleTests.cs @@ -2,7 +2,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class IsMultipleTests { diff --git a/src/FuncSharp.Tests/Collections/IsSingleTests.cs b/src/FuncSharp.Tests/Collections/IsSingleTests.cs index e43444d..5f1fb5e 100644 --- a/src/FuncSharp.Tests/Collections/IsSingleTests.cs +++ b/src/FuncSharp.Tests/Collections/IsSingleTests.cs @@ -2,7 +2,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class IsSingleTests { diff --git a/src/FuncSharp.Tests/Collections/LastOptionTests.cs b/src/FuncSharp.Tests/Collections/LastOptionTests.cs index c74bbff..0914a70 100644 --- a/src/FuncSharp.Tests/Collections/LastOptionTests.cs +++ b/src/FuncSharp.Tests/Collections/LastOptionTests.cs @@ -2,7 +2,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class LastOptionTests { diff --git a/src/FuncSharp.Tests/Collections/SafeConcatTests.cs b/src/FuncSharp.Tests/Collections/SafeConcatTests.cs index 28dcbb0..c8632e3 100644 --- a/src/FuncSharp.Tests/Collections/SafeConcatTests.cs +++ b/src/FuncSharp.Tests/Collections/SafeConcatTests.cs @@ -2,7 +2,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class SafeSafeConcatTests { diff --git a/src/FuncSharp.Tests/Collections/SingleOptionTests.cs b/src/FuncSharp.Tests/Collections/SingleOptionTests.cs index 76b5d43..c42a8fa 100644 --- a/src/FuncSharp.Tests/Collections/SingleOptionTests.cs +++ b/src/FuncSharp.Tests/Collections/SingleOptionTests.cs @@ -2,7 +2,7 @@ using System.Linq; using Xunit; -namespace FuncSharp.Tests.Collections.INonEmptyEnumerable; +namespace FuncSharp.Tests.Collections; public class SingleOptionTests { diff --git a/src/FuncSharp.Tests/Numeric/DivideTests.cs b/src/FuncSharp.Tests/Numeric/DivideTests.cs new file mode 100644 index 0000000..e269d8b --- /dev/null +++ b/src/FuncSharp.Tests/Numeric/DivideTests.cs @@ -0,0 +1,42 @@ +using Xunit; + +namespace FuncSharp.Tests.Numeric; + +public class DivideTests +{ + [Fact] + internal void SafeDivide_int() + { + Assert.Equal(0.5m, 1.SafeDivide(2)); + Assert.Equal(1.5m, 3.SafeDivide(2)); + Assert.Equal(14.33m, 1.SafeDivide(0, 14.33m)); + Assert.Equal(12.12m, 3489.SafeDivide(0, 12.12m)); + } + + [Fact] + internal void SafeDivide_decimal() + { + Assert.Equal(0.5m, 1m.SafeDivide(2)); + Assert.Equal(1.5m, 3m.SafeDivide(2)); + Assert.Equal(14.33m, 1m.SafeDivide(0, 14.33m)); + Assert.Equal(12.12m, 3489m.SafeDivide(0, 12.12m)); + } + + [Fact] + internal void Divide_int() + { + Assert.Equal(0.5m.ToOption(), 1.Divide(2)); + Assert.Equal(1.5m.ToOption(), 3.Divide(2)); + Assert.Equal(Option.Empty(), 1.Divide(0)); + Assert.Equal(Option.Empty(), 3489.Divide(0)); + } + + [Fact] + internal void Divide_decimal() + { + Assert.Equal(0.5m.ToOption(), 1m.Divide(2)); + Assert.Equal(1.5m.ToOption(), 3m.Divide(2)); + Assert.Equal(Option.Empty(), 1m.Divide(0)); + Assert.Equal(Option.Empty(), 3489m.Divide(0)); + } +} \ No newline at end of file diff --git a/src/FuncSharp.Tests/Strings/NonEmptyStringEqualityTests.cs b/src/FuncSharp.Tests/Strings/NonEmptyStringEqualityTests.cs index 8356d69..ff8d7c1 100644 --- a/src/FuncSharp.Tests/Strings/NonEmptyStringEqualityTests.cs +++ b/src/FuncSharp.Tests/Strings/NonEmptyStringEqualityTests.cs @@ -47,8 +47,6 @@ public void EqualityTest() Assert.True(nonEmptyString == text); Assert.True(text.Equals(nonEmptyString)); Assert.True(nonEmptyString.Equals(text)); - Assert.True(text.SafeEquals(nonEmptyString)); - Assert.True(nonEmptyString.SafeEquals(text)); Assert.False(object.Equals(text, nonEmptyString)); // Unfortunately string doesn't override the default Equals method to compare with IEquatable therefore this is false. Assert.True(object.Equals(nonEmptyString, text)); @@ -58,15 +56,11 @@ public void EqualityTest() Assert.False(differentString.Equals(differentNonEmptyString)); Assert.False(differentNonEmptyString == differentString); Assert.False(differentString == differentNonEmptyString); - Assert.False(differentNonEmptyString.SafeEquals(differentString)); - Assert.False(differentString.SafeEquals(differentNonEmptyString)); Assert.False(object.Equals(differentNonEmptyString, differentString)); Assert.False(object.Equals(differentString, differentNonEmptyString)); NonEmptyString null1 = null; string null2 = null; - Assert.True(null1.SafeEquals(null2)); - Assert.True(null2.SafeEquals(null1)); Assert.True(null1 == null2); Assert.True(null2 == null1); Assert.True(object.Equals(null1, null2)); diff --git a/src/FuncSharp/Collections/IEnumerableExtensions_ReadOnlyList.cs b/src/FuncSharp/Collections/IEnumerableExtensions_ReadOnlyList.cs index 15b80f9..47d8856 100644 --- a/src/FuncSharp/Collections/IEnumerableExtensions_ReadOnlyList.cs +++ b/src/FuncSharp/Collections/IEnumerableExtensions_ReadOnlyList.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics.Contracts; namespace FuncSharp; @@ -112,7 +113,7 @@ public static int IndexOf(this IReadOnlyList list, T item) { for (var i = 0; i < list.Count; i++) { - if (list[i].SafeEquals(item)) + if (Equals(list[i], item)) { return i; } diff --git a/src/FuncSharp/Extensions/ObjectExtensions_Generic.cs b/src/FuncSharp/Extensions/ObjectExtensions_Generic.cs index 02c637d..4a8fdba 100644 --- a/src/FuncSharp/Extensions/ObjectExtensions_Generic.cs +++ b/src/FuncSharp/Extensions/ObjectExtensions_Generic.cs @@ -13,32 +13,6 @@ public static INonEmptyEnumerable ToEnumerable(this T value) return NonEmptyEnumerable.Create(value); } - [Pure] - public static bool SafeEquals(this T t, T other) - { - return Equals(t, other); - } - - [Pure] - public static bool SafeNotEquals(this T t, T other) - { - return !t.SafeEquals(other); - } - - [Pure] - public static bool SafeEquals(this T t, T? other) - where T : struct - { - return ((T?)t).SafeEquals(other); - } - - [Pure] - public static bool SafeEquals(this T? t, T other) - where T : struct - { - return t.SafeEquals((T?)other); - } - [DebuggerStepThrough] [Pure] public static void MatchRef(this A a, Action action = null, Action otherwise = null) diff --git a/src/FuncSharp/FuncSharp.csproj b/src/FuncSharp/FuncSharp.csproj index 3b4f82f..9de346e 100644 --- a/src/FuncSharp/FuncSharp.csproj +++ b/src/FuncSharp/FuncSharp.csproj @@ -2,9 +2,9 @@ true CS1591 - 8.0.0 - 8.0.0 - 8.0.0 + 9.0.0 + 9.0.0 + 9.0.0 FuncSharp A C# library with main purpose to reduce boilerplate code and avoid bugs thanks to stronger typing. Utilizes many concepts from functional programming languages that are also applicable in C#. Originally written by Honza Široký. Mews, Honza Široký @@ -13,7 +13,7 @@ https://github.com/MewsSystems/FuncSharp MIT false - Mews methods on collections now crash when the collection is null. Just like LINQ does. Added methods for better working with collections of coproducts, tries and tuples. Added a more performant Match overload for working with IEquatable. + Removed SafeEquals extensions because of performance issues. git https://github.com/MewsSystems/FuncSharp true diff --git a/src/FuncSharp/Numeric/Digit.cs b/src/FuncSharp/Numeric/Digit.cs index 71d62c7..03f10cb 100644 --- a/src/FuncSharp/Numeric/Digit.cs +++ b/src/FuncSharp/Numeric/Digit.cs @@ -36,7 +36,7 @@ public static Digit CreateUnsafe(char value) : null; } - public override bool Equals(object obj) => obj is Digit other && Value.SafeEquals(other.Value); + public override bool Equals(object obj) => obj is Digit other && Value == other.Value; public override int GetHashCode() => Value.GetHashCode(); diff --git a/src/FuncSharp/Numeric/NumberExtensions.cs b/src/FuncSharp/Numeric/NumberExtensions.cs index 085da79..a3692c0 100644 --- a/src/FuncSharp/Numeric/NumberExtensions.cs +++ b/src/FuncSharp/Numeric/NumberExtensions.cs @@ -134,29 +134,31 @@ public static NonPositiveDecimal AsUnsafeNonPositive(this decimal value) #endregion - - - - - - public static decimal SafeDivide(this int a, decimal b, decimal otherwise = 0) { - return Divide(a, b).GetOrElse(otherwise); + return b == 0 + ? otherwise + : a / b; } public static decimal SafeDivide(this decimal a, decimal b, decimal otherwise = 0) { - return Divide(a, b).GetOrElse(otherwise); + return b == 0 + ? otherwise + : a / b; } public static Option Divide(this int a, decimal b) { - return Divide((decimal)a, b); + return b == 0 + ? Option.Empty() + : Option.Valued(a / b); } public static Option Divide(this decimal a, decimal b) { - return b.SafeNotEquals(0).MapTrue(_ => a / b); + return b == 0 + ? Option.Empty() + : Option.Valued(a / b); } } \ No newline at end of file diff --git a/src/FuncSharp/Option/Option.cs b/src/FuncSharp/Option/Option.cs index 9971262..1c2d098 100644 --- a/src/FuncSharp/Option/Option.cs +++ b/src/FuncSharp/Option/Option.cs @@ -257,7 +257,7 @@ public override bool Equals(object obj) { if (obj is Option other) { - return NonEmpty == other.NonEmpty && Value.SafeEquals(other.Value); + return NonEmpty == other.NonEmpty && Equals(Value, other.Value); } if (typeof(A) == typeof(NonEmptyString) && obj is Option otherString) { diff --git a/src/FuncSharp/Strings/NonEmptyStringExtensions.cs b/src/FuncSharp/Strings/NonEmptyStringExtensions.cs index 1da2e49..da9f0ac 100644 --- a/src/FuncSharp/Strings/NonEmptyStringExtensions.cs +++ b/src/FuncSharp/Strings/NonEmptyStringExtensions.cs @@ -6,22 +6,6 @@ namespace FuncSharp; public static class NonEmptyStringExtensions { - [Pure] - public static bool SafeEquals(this NonEmptyString value, string other) - { - if (value is null) - return other is null; - return value.Value.SafeEquals(other); - } - - [Pure] - public static bool SafeNotEquals(this NonEmptyString value, string other) - { - if (value is null) - return other is not null; - return value.Value.SafeNotEquals(other); - } - [Pure] public static string GetOrElse(this Option option, string otherwise) {