diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq.SuperEnumerable.DistinctUntilChanged.md b/Docs/SuperLinq.Docs/apidoc/SuperLinq.SuperEnumerable.DistinctUntilChanged.md new file mode 100644 index 00000000..f8ea57bc --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq.SuperEnumerable.DistinctUntilChanged.md @@ -0,0 +1,27 @@ +--- +uid: SuperLinq.SuperEnumerable.DistinctUntilChanged``1(System.Collections.Generic.IEnumerable{``0}) +example: [*content] +--- +The following code example demonstrates how to get distinct elements from a sequence using `DistinctUntilChanged`. +[!code-csharp[](SuperLinq/DistinctUntilChanged/DistinctUntilChanged1.linq#L6-)] + +--- +uid: SuperLinq.SuperEnumerable.DistinctUntilChanged``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IEqualityComparer{``0}) +example: [*content] +--- +The following code example demonstrates how to get distinct elements from a sequence using `DistinctUntilChanged`. +[!code-csharp[](SuperLinq/DistinctUntilChanged/DistinctUntilChanged2.linq#L6-)] + +--- +uid: SuperLinq.SuperEnumerable.DistinctUntilChanged``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1}) +example: [*content] +--- +The following code example demonstrates how to get distinct elements from a sequence using `DistinctUntilChanged`. +[!code-csharp[](SuperLinq/DistinctUntilChanged/DistinctUntilChanged3.linq#L6-)] + +--- +uid: SuperLinq.SuperEnumerable.DistinctUntilChanged``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IEqualityComparer{``1}) +example: [*content] +--- +The following code example demonstrates how to get distinct elements from a sequence using `DistinctUntilChanged`. +[!code-csharp[](SuperLinq/DistinctUntilChanged/DistinctUntilChanged4.linq#L6-)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged1.linq new file mode 100644 index 00000000..af11f25a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged1.linq @@ -0,0 +1,52 @@ + + SuperLinq + SuperLinq + + +var sequence = new Item[] +{ + new(key: 3, text: "1"), + new(key: 3, text: "2"), + new(key: 2, text: "3"), + new(key: 2, text: "4"), + new(key: 1, text: "5"), + new(key: 1, text: "6"), + new(key: 3, text: "7"), + new(key: 3, text: "8"), + new(key: 2, text: "9"), + new(key: 2, text: "10"), + new(key: 1, text: "11"), + new(key: 1, text: "12"), +}; + +// Get distinct +var result = sequence.DistinctUntilChanged(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(3, 1), (2, 3), (1, 5), (3, 7), (2, 9), (1, 11)] + +class Item : IEquatable +{ + public Item(int key, string text) + { + Key = key; + Text = text; + } + + public int Key { get; } + public string Text { get; } + + public bool Equals(Item other) => + this.Key == other.Key; + + public override int GetHashCode() => + this.Key.GetHashCode(); + + public override string ToString() => + $"({this.Key}, {this.Text})"; +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged2.linq new file mode 100644 index 00000000..c65741a4 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged2.linq @@ -0,0 +1,57 @@ + + SuperLinq + SuperLinq + + +var sequence = new Item[] +{ + new(key: 3, text: "1"), + new(key: 3, text: "2"), + new(key: 2, text: "3"), + new(key: 2, text: "4"), + new(key: 1, text: "5"), + new(key: 1, text: "6"), + new(key: 3, text: "7"), + new(key: 3, text: "8"), + new(key: 2, text: "9"), + new(key: 2, text: "10"), + new(key: 1, text: "11"), + new(key: 1, text: "12"), +}; + +// Get distinct +var result = sequence + .DistinctUntilChanged( + new ItemComparer()); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(3, 1), (2, 3), (1, 5), (3, 7), (2, 9), (1, 11)] + +class Item +{ + public Item(int key, string text) + { + Key = key; + Text = text; + } + + public int Key { get; } + public string Text { get; } + + public override string ToString() => + $"({this.Key}, {this.Text})"; +} + +class ItemComparer : IEqualityComparer +{ + public bool Equals(Item x, Item y) => + x.Key == y.Key; + + public int GetHashCode(Item obj) => + obj.Key.GetHashCode(); +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged3.linq new file mode 100644 index 00000000..e2b588f0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged3.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 3, text: "1"), + (key: 3, text: "2"), + (key: 2, text: "3"), + (key: 2, text: "4"), + (key: 1, text: "5"), + (key: 1, text: "6"), + (key: 3, text: "7"), + (key: 3, text: "8"), + (key: 2, text: "9"), + (key: 2, text: "10"), + (key: 1, text: "11"), + (key: 1, text: "12"), +}; + +// Get distinct +var result = sequence + .DistinctUntilChanged(x => x.key); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(3, 1), (2, 3), (1, 5), (3, 7), (2, 9), (1, 11)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged4.linq new file mode 100644 index 00000000..efcb439e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DistinctUntilChanged/DistinctUntilChanged4.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "aa", text: "1"), + (key: "Aa", text: "2"), + (key: "AA", text: "3"), + (key: "BB", text: "4"), + (key: "bB", text: "5"), + (key: "Cc", text: "6"), + (key: "CC", text: "7"), + (key: "Aa", text: "8"), + (key: "aA", text: "9"), + (key: "bb", text: "10"), + (key: "bB", text: "11"), + (key: "CC", text: "12"), +}; + +// Get distinct +var result = sequence + .DistinctUntilChanged( + x => x.key, + StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aa, 1), (BB, 4), (Cc, 6), (Aa, 8), (bb, 10), (CC, 12)] diff --git a/Source/SuperLinq/DistinctUntilChanged.cs b/Source/SuperLinq/DistinctUntilChanged.cs index c31b0c53..5eb775d8 100644 --- a/Source/SuperLinq/DistinctUntilChanged.cs +++ b/Source/SuperLinq/DistinctUntilChanged.cs @@ -3,25 +3,53 @@ public static partial class SuperEnumerable { /// - /// Returns consecutive distinct elements by using the default equality comparer to compare values. + /// Returns consecutive distinct elements by using the default equality comparer to compare values. /// - /// Source sequence element type. - /// Source sequence. - /// Sequence without adjacent non-distinct elements. - /// is . + /// + /// Source sequence element type. + /// + /// + /// Source sequence. + /// + /// + /// Sequence without adjacent non-distinct elements. + /// + /// + /// is . + /// + /// + /// + /// This method uses deferred execution semantics and streams its results. + /// + /// public static IEnumerable DistinctUntilChanged(this IEnumerable source) { return DistinctUntilChanged(source, Identity, comparer: null); } /// - /// Returns consecutive distinct elements by using the specified equality comparer to compare values. + /// Returns consecutive distinct elements by using the specified equality comparer to compare values. /// - /// Source sequence element type. - /// Source sequence. - /// Comparer used to compare values. - /// Sequence without adjacent non-distinct elements. - /// is . + /// + /// Source sequence element type. + /// + /// + /// Source sequence. + /// + /// + /// Comparer used to compare values. + /// + /// + /// Sequence without adjacent non-distinct elements. + /// + /// + /// is . + /// + /// + /// + /// This method uses deferred execution semantics and streams its results. + /// + /// public static IEnumerable DistinctUntilChanged(this IEnumerable source, IEqualityComparer? comparer) { Guard.IsNotNull(source); @@ -29,32 +57,67 @@ public static IEnumerable DistinctUntilChanged(this IEnumerabl } /// - /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare - /// key values. + /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to + /// compare key values. /// - /// Source sequence element type. - /// Key type. - /// Source sequence. - /// Key selector. - /// Sequence without adjacent non-distinct elements. - /// or is . + /// + /// Source sequence element type. + /// + /// + /// Key type. + /// + /// + /// Source sequence. + /// + /// + /// Key selector. + /// + /// + /// Sequence without adjacent non-distinct elements. + /// + /// + /// or is . + /// + /// + /// + /// This method uses deferred execution semantics and streams its results. + /// + /// public static IEnumerable DistinctUntilChanged(this IEnumerable source, Func keySelector) { return DistinctUntilChanged(source, keySelector, comparer: null); } /// - /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values. + /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to + /// compare key values. /// - /// Source sequence element type. - /// Key type. - /// Source sequence. - /// Key selector. - /// Comparer used to compare key values. - /// Sequence without adjacent non-distinct elements. - /// or is . + /// + /// Source sequence element type. + /// + /// + /// Key type. + /// + /// + /// Source sequence. + /// + /// + /// Key selector. + /// + /// + /// Comparer used to compare key values. + /// + /// + /// Sequence without adjacent non-distinct elements. + /// + /// + /// or is . + /// + /// + /// + /// This method uses deferred execution semantics and streams its results. + /// + /// public static IEnumerable DistinctUntilChanged(this IEnumerable source, Func keySelector, IEqualityComparer? comparer) { Guard.IsNotNull(source);