Skip to content

Commit

Permalink
Merge pull request #822 from polyadic/add-remove-or-none-on-dictionary
Browse files Browse the repository at this point in the history
Add RemoveOrNone to Dictionary extensions
  • Loading branch information
FreeApophis authored Jan 30, 2025
2 parents d425909 + 23746b5 commit e222552
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion FrameworkFeatureConstants.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<DefineConstants>$(DefineConstants);INDEX_OF_CHAR_COMPARISONTYPE_SUPPORTED;TIMESPAN_MULTIPLY_SUPPORTED;SPLIT_ACCEPTS_STRING_SEPARATOR;LAZY_RETURN_CONSTRUCTOR;QUEUE_TRY_OVERLOADS;OPTIMIZED_ELEMENT_AT;RANGE_SUPPORTED;READ_ONLY_SPAN_SUPPORTED;INDEX_TYPE;JOIN_TO_STRING_CHAR_SEPARATOR</DefineConstants>
<DefineConstants>$(DefineConstants);INDEX_OF_CHAR_COMPARISONTYPE_SUPPORTED;TIMESPAN_MULTIPLY_SUPPORTED;SPLIT_ACCEPTS_STRING_SEPARATOR;LAZY_RETURN_CONSTRUCTOR;QUEUE_TRY_OVERLOADS;OPTIMIZED_ELEMENT_AT;RANGE_SUPPORTED;READ_ONLY_SPAN_SUPPORTED;INDEX_TYPE;JOIN_TO_STRING_CHAR_SEPARATOR;REMOVE_EXTENSION</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netcoreapp3.0'))">
<DefineConstants>$(DefineConstants);SYSTEM_INDEX_SUPPORTED;IP_END_POINT_TRY_PARSE_SUPPORTED</DefineConstants>
Expand Down
24 changes: 24 additions & 0 deletions Funcky.Test/Extensions/DictionaryExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,28 @@ public void CallingGetValueOrNoneOnADictionaryThatImplementsBothReadonlyAndNonRe
var dictionary = new Dictionary<string, string> { ["some"] = "value" };
_ = dictionary.GetValueOrNone("some");
}

#if REMOVE_EXTENSION
[Fact]
public void RemoveOrNoneOfAnExistingItemRemovesItemFromDictionaryAndReturnsTheValue()
{
IDictionary<string, string> dictionary = new Dictionary<string, string> { ["some"] = "value" };

var value = dictionary.RemoveOrNone("some");

Assert.Empty(dictionary);
FunctionalAssert.Some("value", value);
}

[Fact]
public void RemoveOrNoneOfANonExistingItemReturnsNoneAndLeavesTheDictionaryUnchanegd()
{
IDictionary<string, string> dictionary = new Dictionary<string, string> { ["some"] = "value" };

var value = dictionary.RemoveOrNone("none");

Assert.NotEmpty(dictionary);
FunctionalAssert.None(value);
}
#endif
}
5 changes: 3 additions & 2 deletions Funcky.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build Config", "Build Config", "{DD8F8450-BE23-4D6B-9C5C-7AED0ABB7531}"
ProjectSection(SolutionItems) = preProject
Analyzers.props = Analyzers.props
Funcky\CompatibilitySuppressions.xml = Funcky\CompatibilitySuppressions.xml
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
FrameworkFeatureConstants.props = FrameworkFeatureConstants.props
global.json = global.json
GlobalUsings.props = GlobalUsings.props
GlobalUsings.Test.props = GlobalUsings.Test.props
NuGet.config = NuGet.config
typos.toml = typos.toml
Analyzers.props = Analyzers.props
SemanticVersioning.targets = SemanticVersioning.targets
typos.toml = typos.toml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Funcky.Xunit", "Funcky.Xunit\Funcky.Xunit.csproj", "{F2E98B0D-CC17-4576-89DE-065FF475BE6E}"
Expand Down
6 changes: 6 additions & 0 deletions Funcky/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
<Left>lib/netstandard2.1/Funcky.dll</Left>
<Right>lib/netcoreapp3.1/Funcky.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0021</DiagnosticId>
<Target>M:Funcky.Extensions.DictionaryExtensions.RemoveOrNone``2(System.Collections.Generic.IDictionary{``0,``1},``0)``0:notnull</Target>
<Left>lib/netstandard2.1/Funcky.dll</Left>
<Right>lib/netcoreapp3.1/Funcky.dll</Right>
</Suppression>
</Suppressions>
13 changes: 13 additions & 0 deletions Funcky/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ public static Option<TValue> GetValueOrNone<TKey, TValue>(this IReadOnlyDictiona
=> dictionary.TryGetValue(readOnlyKey, out var result)
? result
: Option<TValue>.None;

#if REMOVE_EXTENSION
public static Option<TValue> RemoveOrNone<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
#if NETCOREAPP3_1
// TKey was constraint to notnull when nullability annotations were originally added. It was later dropped again.
// See: https://github.com/dotnet/runtime/issues/31401
where TKey : notnull
#endif
where TValue : notnull
=> dictionary.Remove(key, out var result)
? result
: Option<TValue>.None;
#endif
}
1 change: 1 addition & 0 deletions Funcky/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable
Funcky.Extensions.JsonSerializerOptionsExtensions
Funcky.Extensions.OrderedDictionaryExtensions
static Funcky.Extensions.DictionaryExtensions.RemoveOrNone<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue>! dictionary, TKey key) -> Funcky.Monads.Option<TValue>
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, T4, T5, TResult>(this System.Func<T1, T2, T3, T4, T5, TResult>! func, Funcky.Unit p1, Funcky.Unit p2, Funcky.Unit p3, Funcky.Unit p4, T5 p5) -> System.Func<T1, T2, T3, T4, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, T4, T5, TResult>(this System.Func<T1, T2, T3, T4, T5, TResult>! func, Funcky.Unit p1, Funcky.Unit p2, Funcky.Unit p3, T4 p4, Funcky.Unit p5) -> System.Func<T1, T2, T3, T5, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, T4, T5, TResult>(this System.Func<T1, T2, T3, T4, T5, TResult>! func, Funcky.Unit p1, Funcky.Unit p2, Funcky.Unit p3, T4 p4, T5 p5) -> System.Func<T1, T2, T3, TResult>!
Expand Down

0 comments on commit e222552

Please sign in to comment.