diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 969b5fd..cf0d86d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,15 +5,13 @@ on: branches: [ "master" ] pull_request: branches: [ "master" ] + workflow_dispatch: jobs: build: - runs-on: ubuntu-latest + runs-on: windows-latest steps: - uses: actions/checkout@v4 - - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 8.0.x - run: dotnet restore - run: dotnet build --no-restore - run: dotnet test --no-build --verbosity normal diff --git a/Xledger.Collections.Test/TestImmDict.cs b/Xledger.Collections.Test/TestImmDict.cs index 1cc1466..56ec324 100644 --- a/Xledger.Collections.Test/TestImmDict.cs +++ b/Xledger.Collections.Test/TestImmDict.cs @@ -71,6 +71,34 @@ public void TestNoOps() { Assert.ThrowsAny(() => idict.Remove(4)); Assert.ThrowsAny(() => ((System.Collections.ICollection)idict).CopyTo((Array)null, 0)); } + + [Fact] + public void TestSelectKey() { + var dct = Enumerable.Range(-100, 1_000).ToDictionary(i => i); + var imm = Enumerable.Range(-100, 1_000).ToImmDict(i => i); + + for (int i = -1000; i < 2000; ++i) { + Assert.Equal(dct.ContainsKey(i), imm.ContainsKey(i)); + if (dct.TryGetValue(i, out var dctValue)) { + imm.TryGetValue(i, out var immValue); + Assert.Equal(dctValue, immValue); + } + } + } + + [Fact] + public void TestSelectKeySelectValue() { + var dct = Enumerable.Range(-100, 1_000).ToDictionary(i => i, i => (i * i).ToString()); + var imm = Enumerable.Range(-100, 1_000).ToImmDict(i => i, i => (i * i).ToString()); + + for (int i = -1000; i < 2000; ++i) { + Assert.Equal(dct.ContainsKey(i), imm.ContainsKey(i)); + if (dct.TryGetValue(i, out var dctValue)) { + imm.TryGetValue(i, out var immValue); + Assert.Equal(dctValue, immValue); + } + } + } } diff --git a/Xledger.Collections.Test/Xledger.Collections.Test.csproj b/Xledger.Collections.Test/Xledger.Collections.Test.csproj index 6390632..649b7be 100644 --- a/Xledger.Collections.Test/Xledger.Collections.Test.csproj +++ b/Xledger.Collections.Test/Xledger.Collections.Test.csproj @@ -3,7 +3,7 @@ Xledger.Collections.Test - net48;net6.0;net8.0 + net48;net8.0 12.0 disable disable diff --git a/Xledger.Collections/Extensions.cs b/Xledger.Collections/Extensions.cs index 223eebd..e5caab7 100644 --- a/Xledger.Collections/Extensions.cs +++ b/Xledger.Collections/Extensions.cs @@ -47,6 +47,23 @@ public static ImmDict ToImmDict(this IEnumerable<(K, V)> xs) { }; } + public static ImmDict ToImmDict(this IEnumerable xs, Func selectKey) { + return xs switch { + null => ImmDict.Empty, + _ => new ImmDict(xs.ToDictionary(selectKey)), + }; + } + + public static ImmDict ToImmDict( + this IEnumerable xs, + Func selectKey, + Func selectVal + ) { + return xs switch { + null => ImmDict.Empty, + _ => new ImmDict(xs.ToDictionary(selectKey, selectVal)), + }; + } internal static T[] ArrayOf(T[] arr) { return (T[])arr.Clone(); } diff --git a/Xledger.Collections/Xledger.Collections.csproj b/Xledger.Collections/Xledger.Collections.csproj index 8b7865a..f2d8387 100644 --- a/Xledger.Collections/Xledger.Collections.csproj +++ b/Xledger.Collections/Xledger.Collections.csproj @@ -5,7 +5,7 @@ Xledger.Collections Xledger.Collections - net48;net6.0;net8.0 + net48;net8.0 12.0 disable disable