diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateBy/AggregateBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateBy/AggregateBy1.linq new file mode 100644 index 000000000..979f80d34 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateBy/AggregateBy1.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 19); + +// Aggregate elements in a sequence grouped by key +var result = sequence + .AggregateBy( + x => x % 3, + 0, + (acc, e) => acc + e); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [[1, 70], [2, 57], [0, 63]] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateBy/AggregateBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateBy/AggregateBy2.linq new file mode 100644 index 000000000..9bc4edaf8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateBy/AggregateBy2.linq @@ -0,0 +1,21 @@ + + C:\Users\stuar\source\repos\viceroypenguin\SuperLinq\Source\SuperLinq\bin\Release\net7.0\SuperLinq.dll + SuperLinq + + +var sequence = Enumerable.Range(1, 19); + +// Aggregate elements in a sequence grouped by key +var result = sequence + .AggregateBy( + x => x % 3, + k => k * 1_000, + (acc, e) => acc + e); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [[1, 1070], [2, 2057], [0, 63]] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight1.linq new file mode 100644 index 000000000..c78c98b6e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight1.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var numbers = new string[] { "1", "2", "3", "4", "5", }; + +// Enumerate strings from right to left and collect the text into larger strings. +var result = numbers + .AggregateRight((a, b) => $"({a}/{b})"); + +Console.WriteLine(result); + +// This code produces the following output: +// (1/(2/(3/(4/5)))) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight2.linq new file mode 100644 index 000000000..0fed4aa26 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight2.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var numbers = new string[] { "1", "2", "3", "4", "5", }; + +// Enumerate strings from right to left and collect the text into larger strings. +var result = numbers + .AggregateRight("6", (a, b) => $"({a}/{b})"); + +Console.WriteLine(result); + +// This code produces the following output: +// (1/(2/(3/(4/(5/6))))) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight3.linq new file mode 100644 index 000000000..93123483c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AggregateRight/AggregateRight3.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var numbers = new string[] { "1", "2", "3", "4", "5", }; + +// Enumerate strings from right to left and collect the text into larger strings. +var result = numbers + .AggregateRight( + "6", + (a, b) => $"({a}/{b})", + s => s.Length); + +Console.WriteLine(result); + +// This code produces the following output: +// 21 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/AssertCount/AssertCount.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AssertCount/AssertCount.linq new file mode 100644 index 000000000..03b5bfc3e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AssertCount/AssertCount.linq @@ -0,0 +1,28 @@ + + SuperLinq + SuperLinq + + +var numbers = new string[] { "1", "2", "3", "4", "5", }; + +// Enumerate the sequence with a valid length. +// This code executes normally. +numbers + .AssertCount(5) + .Consume(); + +// Enumerate the sequence with an invalid length. +// This code throws an `ArgumentException` +try +{ + numbers + .AssertCount(4) + .Consume(); +} +catch (ArgumentException ae) +{ + Console.WriteLine(ae.Message); + + // This code produces the following output: + // Parameter "source.Count()" (int) must be equal to <4>, was <5>. (Parameter 'source.Count()') +} \ No newline at end of file diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/AtLeast/AtLeast.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AtLeast/AtLeast.linq new file mode 100644 index 000000000..a7f4479a7 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AtLeast/AtLeast.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +foreach (var x in Enumerable.Range(3, 5)) +{ + // Check that a sequence has a minimum size + var result = sequence.AtLeast(x); + + Console.WriteLine($"AtLeast {x}: {result}"); +} + +// This code produces the following output: +// AtLeast 3: True +// AtLeast 4: True +// AtLeast 5: True +// AtLeast 6: False +// AtLeast 7: False diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/AtMost/AtMost.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AtMost/AtMost.linq new file mode 100644 index 000000000..5994e7929 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/AtMost/AtMost.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +foreach (var x in Enumerable.Range(3, 5)) +{ + // Check that a sequence has a maximum length + var result = sequence.AtMost(x); + + Console.WriteLine($"AtMost {x}: {result}"); +} + +// This code produces the following output: +// AtMost 3: False +// AtMost 4: False +// AtMost 5: True +// AtMost 6: True +// AtMost 7: True diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Backsert/Backsert.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Backsert/Backsert.linq new file mode 100644 index 000000000..ed3ec49a2 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Backsert/Backsert.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var first = Enumerable.Range(1, 5); +var second = Enumerable.Range(1, 5); + +// Insert one sequence into another +var result = first + .Backsert(second, 2); + +Console.WriteLine(string.Join(", ", result)); + +// This code produces the following output: +// 1, 2, 3, 1, 2, 3, 4, 5, 4, 5 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch1.linq new file mode 100644 index 000000000..e421cba03 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch1.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into three chunks of 3 and one chunk of 1 +var result = sequence.Batch(3); + +Console.WriteLine( + "[" + + string.Join( + ", ", + result.Select(c => "[" + string.Join(", ", c) + "]")) + + "]"); + +// This code produces the following output: +// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch2.linq new file mode 100644 index 000000000..ec1a4dec9 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch2.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into three chunks of 3 and one chunk of 1 +var result = sequence + .Batch( + 3, + c => "[" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch3.linq new file mode 100644 index 000000000..b0d931d1a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch3.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into three chunks of 3 and one chunk of 1 +var buffer = new int[3]; +var result = sequence + .Batch( + buffer, + c => "[" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch4.linq new file mode 100644 index 000000000..4af1780ce --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Batch/Batch4.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into three chunks of 3 and one chunk of 1 +var buffer = new int[5]; +var result = sequence + .Batch( + buffer, + 3, + c => "[" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/BindByIndex/BindByIndex1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/BindByIndex/BindByIndex1.linq new file mode 100644 index 000000000..80425e0b8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/BindByIndex/BindByIndex1.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); +var indices = new int[] { 0, 1, 8, 9, 3, 4, 2, }; + +// Select elements from sequence using the values in indices +var result = sequence + .BindByIndex(indices); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 9, 10, 4, 5, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/BindByIndex/BindByIndex2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/BindByIndex/BindByIndex2.linq new file mode 100644 index 000000000..e8e477fd9 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/BindByIndex/BindByIndex2.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); +var indices = new int[] { 0, 1, 8, 10, 3, 4, 2, }; + +// Select elements from sequence using the values in indices +var result = sequence + .BindByIndex( + indices, + (e, i) => e.ToString(), + i => "null"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 9, null, 4, 5, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Buffer/Buffer1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Buffer/Buffer1.linq new file mode 100644 index 000000000..130c72f2c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Buffer/Buffer1.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into three chunks of 3 and one chunk of 1 +var result = sequence.Buffer(3); + +Console.WriteLine( + "[" + + string.Join( + ", ", + result.Select(c => "[" + string.Join(", ", c) + "]")) + + "]"); + +// This code produces the following output: +// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Buffer/Buffer2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Buffer/Buffer2.linq new file mode 100644 index 000000000..b408ae632 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Buffer/Buffer2.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into overlapping chunks of size 3 +var result = sequence.Buffer(3, 2); + +Console.WriteLine( + "[" + + string.Join( + ", ", + result.Select(c => "[" + string.Join(", ", c) + "]")) + + "]"); + +// This code produces the following output: +// [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Cartesian/Cartesian.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Cartesian/Cartesian.linq new file mode 100644 index 000000000..c59aee9b1 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Cartesian/Cartesian.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { 1, 2, 3, }; +var seq2 = new[] { "foo", "bar", "quz", }; + +// Take a slice of the sequence +var result = seq1.Cartesian(seq2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Case/Case1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Case/Case1.linq new file mode 100644 index 000000000..0f33f799c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Case/Case1.linq @@ -0,0 +1,27 @@ + + SuperLinq + SuperLinq + + +var sequences = Enumerable.Range(1, 5) + .ToDictionary( + x => x, + x => Enumerable.Range(1, x)); + +// Use a function to select which sequence to return values from. +var selector = 1; +var result = SuperEnumerable + .Case( + () => selector, + sequences); + +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); +selector = 4; +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); +selector = 2; +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); + +// This code produces the following output: +// Selector: 1; result.Count(): 1. +// Selector: 4; result.Count(): 4. +// Selector: 2; result.Count(): 2. diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Case/Case2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Case/Case2.linq new file mode 100644 index 000000000..2625800bb --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Case/Case2.linq @@ -0,0 +1,28 @@ + + SuperLinq + SuperLinq + + +var sequences = Enumerable.Range(1, 5) + .ToDictionary( + x => x, + x => Enumerable.Range(1, x)); + +// Use a function to select which sequence to return values from. +var selector = 1; +var result = SuperEnumerable + .Case( + () => selector, + sequences, + Enumerable.Range(1, 100)); + +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); +selector = 4; +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); +selector = 20; +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); + +// This code produces the following output: +// Selector: 1; result.Count(): 1. +// Selector: 4; result.Count(): 4. +// Selector: 20; result.Count(): 100. diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch1.linq new file mode 100644 index 000000000..d1400737b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch1.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 6th element +var sequence = Enumerable.Range(1, 5).Select(i => i.ToString()) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Use a function to determine how to handle an exception +var result = sequence + .Catch( + (InvalidOperationException ex) => SuperEnumerable.Return(ex.Message)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, Operation is not valid due to the current state of the object.] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch2.linq new file mode 100644 index 000000000..49515a782 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch2.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 6th element +var sequence = Enumerable.Range(1, 5) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Use a second sequence to continue in the case of an exception +var result = sequence + .Catch(Enumerable.Range(1, 5)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch3.linq new file mode 100644 index 000000000..bef0bdb91 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch3.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 6th element +var sequence = Enumerable.Range(1, 5) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Use a series of sequences to enumerate until one completes successfully. +var result = SuperEnumerable + .Catch( + sequence, + sequence, + Enumerable.Range(1, 3), + Enumerable.Range(1, 10)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch4.linq new file mode 100644 index 000000000..6286b7c6b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Catch/Catch4.linq @@ -0,0 +1,27 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 6th element +var sequence = Enumerable.Range(1, 5) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Use a series of sequences to enumerate until one completes successfully. +var result = SuperEnumerable + .Catch( + new List> + { + sequence, + sequence, + Enumerable.Range(1, 3), + Enumerable.Range(1, 10), + }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Choose/Choose.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Choose/Choose.linq new file mode 100644 index 000000000..99d489fb0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Choose/Choose.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = "O,l,2,3,4,S,6,7,B,9".Split(','); + +// Use a function to choose and project elements in the sequence +var result = sequence + .Choose(s => (int.TryParse(s, out var n), n)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [2, 3, 4, 6, 7, 9] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CollectionEqual/CollectionEqual1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CollectionEqual/CollectionEqual1.linq new file mode 100644 index 000000000..b4e6127d0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CollectionEqual/CollectionEqual1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var pets1 = new List { new("Turbo", 2), new("Peanut", 8), }; +var pets2 = new List { new("Peanut", 8), new("Turbo", 2), }; + +// Determine if the two collections are equal, using the default equality comparer +var result = pets1.CollectionEqual(pets2); + +Console.WriteLine(result); + +// This code produces the following output: +// True + +record Pet(string Name, int Age); \ No newline at end of file diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CollectionEqual/CollectionEqual2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CollectionEqual/CollectionEqual2.linq new file mode 100644 index 000000000..cbee8e3a7 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CollectionEqual/CollectionEqual2.linq @@ -0,0 +1,39 @@ + + SuperLinq + SuperLinq + + +var pets1 = new List { new("Turbo", 2), new("Peanut", 8), }; +var pets2 = new List { new("Peanut", 8), new("Turbo", 2), }; + +// Determine if the two collections are equal, using a custom equality comparer +var result = pets1 + .CollectionEqual( + pets2, + new PetComparer()); + +Console.WriteLine(result); + +// This code produces the following output: +// True + +class Pet +{ + public Pet(string name, int age) + { + this.Name = name; + this.Age = age; + } + + public string Name { get; } + public int Age { get; } +} + +class PetComparer : IEqualityComparer +{ + public bool Equals(Pet x, Pet y) => + x.Name == y.Name + && x.Age == y.Age; + public int GetHashCode(Pet x) => + HashCode.Combine(x.Name, x.Age); +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CompareCount/CompareCount.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CompareCount/CompareCount.linq new file mode 100644 index 000000000..67b716bb9 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CompareCount/CompareCount.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +foreach (var x in Enumerable.Range(3, 5)) +{ + // Compare the length of two sequences + var result = sequence.CompareCount(Enumerable.Range(1, x)); + + Console.WriteLine($"CompareCount {x}: {result}"); +} + +// This code produces the following output: +// CompareCount 3: 1 +// CompareCount 4: 1 +// CompareCount 5: 0 +// CompareCount 6: -1 +// CompareCount 7: -1 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Consume/Consume.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Consume/Consume.linq new file mode 100644 index 000000000..000797f0d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Consume/Consume.linq @@ -0,0 +1,13 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5) + .Do(i => Console.Write($"{i}, ")); + +// Consume the provided sequence +sequence.Consume(); + +// This code produces the following output: +// 1, 2, 3, 4, 5, diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo1.linq new file mode 100644 index 000000000..135457283 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo1.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); +var destination = new List() { -1, -2, }; + +// Copy the provided sequence to a list +var result = sequence.CopyTo(destination); + +Console.WriteLine(result); +Console.WriteLine( + "[" + + string.Join(", ", destination) + + "]"); + +// This code produces the following output: +// 5 +// [1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo2.linq new file mode 100644 index 000000000..c5d7f020b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo2.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); +var destination = new List() { -1, -2, }; + +// Copy the provided sequence to a list at a specified index +var result = sequence.CopyTo(destination, 2); + +Console.WriteLine(result); +Console.WriteLine( + "[" + + string.Join(", ", destination) + + "]"); + +// This code produces the following output: +// 5 +// [-1, -2, 1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo3.linq new file mode 100644 index 000000000..14e6763aa --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo3.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); +var destination = new int[7]; +var span = destination.AsSpan(); + +// Copy the provided sequence to a span +var result = sequence.CopyTo(span[1..]); + +Console.WriteLine(result); +Console.WriteLine( + "[" + + string.Join(", ", destination) + + "]"); + +// This code produces the following output: +// 5 +// [0, 1, 2, 3, 4, 5, 0] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo4.linq new file mode 100644 index 000000000..dec6f199d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CopyTo/CopyTo4.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); +var destination = new int[5]; + +// Copy the provided sequence to an array +var result = sequence.CopyTo(destination); + +Console.WriteLine(result); +Console.WriteLine( + "[" + + string.Join(", ", destination) + + "]"); + +// This code produces the following output: +// 5 +// [1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBetween/CountBetween.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBetween/CountBetween.linq new file mode 100644 index 000000000..991a1b402 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBetween/CountBetween.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +foreach (var x in Enumerable.Range(3, 5)) +{ + // Check that a sequence has a length between two numbers + var result = sequence.CountBetween(x, x + 1); + + Console.WriteLine($"CountBetween {x}-{x + 1}: {result}"); +} + +// This code produces the following output: +// CountBetween 3-4: False +// CountBetween 4-5: True +// CountBetween 5-6: True +// CountBetween 6-7: False +// CountBetween 7-8: False diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBy/CountBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBy/CountBy1.linq new file mode 100644 index 000000000..9f966e198 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBy/CountBy1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 19); + +// Count elements in a sequence grouped by key +var result = sequence.CountBy(x => x % 3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 7), (2, 6), (0, 6)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBy/CountBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBy/CountBy2.linq new file mode 100644 index 000000000..939337a3b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountBy/CountBy2.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "a", "B", "c", "A", "b", "A", }; + +// Count elements in a sequence grouped by key +var result = sequence.CountBy(SuperEnumerable.Identity, StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(a, 3), (B, 2), (c, 1)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountDown/CountDown1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountDown/CountDown1.linq new file mode 100644 index 000000000..c01bc2334 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountDown/CountDown1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Get a countdown counter for the the sequence +var result = sequence.CountDown(2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, ), (2, ), (3, ), (4, 1), (5, 0)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountDown/CountDown2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountDown/CountDown2.linq new file mode 100644 index 000000000..f958eb167 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/CountDown/CountDown2.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Get a countdown counter for the the sequence +var result = sequence.CountDown(2, (item, cd) => new { Item = item, CountDown = cd?.ToString() ?? "null", }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ Item = 1, CountDown = null }, { Item = 2, CountDown = null }, { Item = 3, CountDown = null }, { Item = 4, CountDown = 1 }, { Item = 5, CountDown = 0 }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Defer/Defer.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Defer/Defer.linq new file mode 100644 index 000000000..7ac256e0e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Defer/Defer.linq @@ -0,0 +1,28 @@ + + SuperLinq + SuperLinq + + +var count = 3; + +// Use a function to create a sequence at execution time +var result = SuperEnumerable + .Defer(() => Enumerable.Range(1, count)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// changing count changes the length of the sequence +// returned by the function given to Defer +count = 5; + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3] +// [1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort1.linq new file mode 100644 index 000000000..1b6d5e70b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort1.linq @@ -0,0 +1,47 @@ + + SuperLinq + SuperLinq + + +var sequence = new Item[] +{ + new(key: 5, text: "1"), + new(key: 5, text: "2"), + new(key: 4, text: "3"), + new(key: 4, text: "4"), + new(key: 3, text: "5"), + new(key: 3, text: "6"), + new(key: 2, text: "7"), + new(key: 2, text: "8"), + new(key: 1, text: "9"), + new(key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence.DensePartialSort(3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 9), (1, 10), (2, 7), (2, 8), (3, 5), (3, 6)] + +class Item : IComparable +{ + public Item(int key, string text) + { + Key = key; + Text = text; + } + + public int Key { get; } + public string Text { get; } + + public int CompareTo(Item other) => + this.Key.CompareTo(other.Key); + + public override string ToString() => + $"({this.Key}, {this.Text})"; +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort2.linq new file mode 100644 index 000000000..29d89d5de --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort2.linq @@ -0,0 +1,47 @@ + + SuperLinq + SuperLinq + + +var sequence = new Item[] +{ + new(key: 5, text: "1"), + new(key: 5, text: "2"), + new(key: 4, text: "3"), + new(key: 4, text: "4"), + new(key: 3, text: "5"), + new(key: 3, text: "6"), + new(key: 2, text: "7"), + new(key: 2, text: "8"), + new(key: 1, text: "9"), + new(key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence.DensePartialSort(3, OrderByDirection.Descending); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, 1), (5, 2), (4, 3), (4, 4), (3, 5), (3, 6)] + +class Item : IComparable +{ + public Item(int key, string text) + { + Key = key; + Text = text; + } + + public int Key { get; } + public string Text { get; } + + public int CompareTo(Item other) => + this.Key.CompareTo(other.Key); + + public override string ToString() => + $"({this.Key}, {this.Text})"; +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort3.linq new file mode 100644 index 000000000..362f7076a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort3.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DensePartialSort( + 3, + Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key))); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 9), (1, 10), (2, 7), (2, 8), (3, 5), (3, 6)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort4.linq new file mode 100644 index 000000000..dbb96390f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSort/DensePartialSort4.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DensePartialSort( + 3, + Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key)), + OrderByDirection.Descending); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, 1), (5, 2), (4, 3), (4, 4), (3, 5), (3, 6)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy1.linq new file mode 100644 index 000000000..238b601c6 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy1.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DensePartialSortBy( + 3, + x => x.key); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 9), (1, 10), (2, 7), (2, 8), (3, 5), (3, 6)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy2.linq new file mode 100644 index 000000000..537c94b79 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy2.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DensePartialSortBy( + 3, + x => x.key, + OrderByDirection.Descending); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, 1), (5, 2), (4, 3), (4, 4), (3, 5), (3, 6)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy3.linq new file mode 100644 index 000000000..0690b5743 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy3.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DensePartialSortBy( + 1, + x => x.key, + Comparer.Create((x, y) => (x % 2).CompareTo(y % 2))); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(4, 3), (4, 4), (2, 7), (2, 8)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy4.linq new file mode 100644 index 000000000..c1c020361 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DensePartialSortBy/DensePartialSortBy4.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DensePartialSortBy( + 1, + x => x.key, + Comparer.Create((x, y) => (x % 2).CompareTo(y % 2)), + OrderByDirection.Descending); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, 1), (5, 2), (3, 5), (3, 6), (1, 9), (1, 10)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRank/DenseRank1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRank/DenseRank1.linq new file mode 100644 index 000000000..f75865f43 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRank/DenseRank1.linq @@ -0,0 +1,47 @@ + + SuperLinq + SuperLinq + + +var sequence = new Item[] +{ + new(key: 5, text: "1"), + new(key: 5, text: "2"), + new(key: 4, text: "3"), + new(key: 4, text: "4"), + new(key: 3, text: "5"), + new(key: 3, text: "6"), + new(key: 2, text: "7"), + new(key: 2, text: "8"), + new(key: 1, text: "9"), + new(key: 1, text: "10"), +}; + +// Rank the items in the sequence +var result = sequence.DenseRank(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [((1, 9), 1), ((1, 10), 1), ((2, 7), 2), ((2, 8), 2), ((3, 5), 3), ((3, 6), 3), ((4, 3), 4), ((4, 4), 4), ((5, 1), 5), ((5, 2), 5)] + +class Item : IComparable +{ + public Item(int key, string text) + { + Key = key; + Text = text; + } + + public int Key { get; } + public string Text { get; } + + public int CompareTo(Item other) => + this.Key.CompareTo(other.Key); + + public override string ToString() => + $"({this.Key}, {this.Text})"; +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRank/DenseRank3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRank/DenseRank3.linq new file mode 100644 index 000000000..fdcf1b042 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRank/DenseRank3.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DenseRank( + Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key))); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [((1, 9), 1), ((1, 10), 1), ((2, 7), 2), ((2, 8), 2), ((3, 5), 3), ((3, 6), 3), ((4, 3), 4), ((4, 4), 4), ((5, 1), 5), ((5, 2), 5)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRankBy/DenseRankBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRankBy/DenseRankBy1.linq new file mode 100644 index 000000000..4124f92f4 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRankBy/DenseRankBy1.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DenseRankBy( + x => x.key); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [((1, 9), 1), ((1, 10), 1), ((2, 7), 2), ((2, 8), 2), ((3, 5), 3), ((3, 6), 3), ((4, 3), 4), ((4, 4), 4), ((5, 1), 5), ((5, 2), 5)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRankBy/DenseRankBy3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRankBy/DenseRankBy3.linq new file mode 100644 index 000000000..b2ceeeffc --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DenseRankBy/DenseRankBy3.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N sets of items +var result = sequence + .DenseRankBy( + x => x.key, + Comparer.Create((x, y) => (x % 2).CompareTo(y % 2))); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [((4, 3), 1), ((4, 4), 1), ((2, 7), 1), ((2, 8), 1), ((5, 1), 2), ((5, 2), 2), ((3, 5), 2), ((3, 6), 2), ((1, 9), 2), ((1, 10), 2)] 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 000000000..af11f25a6 --- /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 000000000..c65741a45 --- /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 000000000..e2b588f0e --- /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 000000000..efcb439ec --- /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/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do1.linq new file mode 100644 index 000000000..3099001ad --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Execute an action for each element +var result = sequence + .Do(i => Console.Write($"{i}, ")); + +Console.WriteLine("Before"); +result.Consume(); +Console.WriteLine("After"); + +// This code produces the following output: +// Before +// 1, 2, 3, 4, 5, After diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do2.linq new file mode 100644 index 000000000..f1fcf5e81 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do2.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Execute an action for each element, and on completion +var result = sequence + .Do( + i => Console.Write($"{i}, "), + () => Console.WriteLine("Completed")); + +Console.WriteLine("Before"); +result.Consume(); +Console.WriteLine("After"); + +// This code produces the following output: +// Before +// 1, 2, 3, 4, 5, Completed +// After diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do3.linq new file mode 100644 index 000000000..12466a0c5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do3.linq @@ -0,0 +1,25 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 4).Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Execute an action for each element, on error, and on completion +var result = sequence + .Do( + i => Console.Write($"{i}, "), + ex => Console.WriteLine("Failed: " + ex.Message)); + +Console.WriteLine("Before"); +try +{ + result.Consume(); +} +catch (InvalidOperationException) {} +Console.WriteLine("After"); + +// This code produces the following output: +// Before +// 1, 2, 3, 4, Failed: Operation is not valid due to the current state of the object. +// After diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do4.linq new file mode 100644 index 000000000..3fdbe2d16 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Do/Do4.linq @@ -0,0 +1,38 @@ + + SuperLinq + SuperLinq + + +var flag = false; +var sequence = SuperEnumerable.If( + () => flag, + Enumerable.Range(1, 5), + Enumerable.Range(1, 4).Concat(SuperEnumerable.Throw(new InvalidOperationException()))); + +// Execute an action for each element, on error, and on completion +var result = sequence + .Do( + i => Console.Write($"{i}, "), + ex => Console.WriteLine("Failed: " + ex.Message), + () => Console.WriteLine("Completed")); + +Console.WriteLine("Before 1"); +try +{ + result.Consume(); +} +catch (InvalidOperationException) {} +Console.WriteLine("After 1"); + +flag = true; +Console.WriteLine("Before 2"); +result.Consume(); +Console.WriteLine("After 2"); + +// This code produces the following output: +// Before 1 +// 1, 2, 3, 4, Failed: Operation is not valid due to the current state of the object. +// After 1 +// Before 2 +// 1, 2, 3, 4, 5, Completed +// After 2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/DoWhile/DoWhile.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DoWhile/DoWhile.linq new file mode 100644 index 000000000..ca06a0333 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/DoWhile/DoWhile.linq @@ -0,0 +1,27 @@ + + SuperLinq + SuperLinq + + +var executionCount = 0; +var sequence = Enumerable.Range(1, 5) + .Do(_ => executionCount++); + +// Execute an action for each element +var loopCount = 0; +var result = sequence + .DoWhile(() => loopCount++ < 2); + +Console.WriteLine($"Before (execCount: {executionCount})"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +Console.WriteLine($"After (execCount: {executionCount})"); + +// This code produces the following output: +// Before (execCount: 0) +// [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] +// After (execCount: 5) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/EndsWith/EndsWith1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EndsWith/EndsWith1.linq new file mode 100644 index 000000000..51b139c3c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EndsWith/EndsWith1.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Determine if sequence ends with the ends sequence +var ends = Enumerable.Range(4, 2); +var result = sequence.EndsWith(ends); + +Console.WriteLine($"EndsWith: {result}"); + +// This code produces the following output: +// EndsWith: True diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/EndsWith/EndsWith2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EndsWith/EndsWith2.linq new file mode 100644 index 000000000..985743cde --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EndsWith/EndsWith2.linq @@ -0,0 +1,39 @@ + + SuperLinq + SuperLinq + + +var pets1 = new List { new("Turbo", 2), new("Peanut", 8), }; +var pets2 = new List { new("Peanut", 8), }; + +// Determine if pets1 ends with the pets2 sequence. +var result = pets1 + .EndsWith( + pets2, + new PetComparer()); + +Console.WriteLine($"EndsWith: {result}"); + +// This code produces the following output: +// EndsWith: True + +class Pet +{ + public Pet(string name, int age) + { + this.Name = name; + this.Age = age; + } + + public string Name { get; } + public int Age { get; } +} + +class PetComparer : IEqualityComparer +{ + public bool Equals(Pet x, Pet y) => + x.Name == y.Name + && x.Age == y.Age; + public int GetHashCode(Pet x) => + HashCode.Combine(x.Name, x.Age); +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip1.linq new file mode 100644 index 000000000..3726a59e8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, }; + +// Determine if sequence ends with the ends sequence +var result = seq1.EquiZip(seq2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1), (bb, 2), (c, 3), (ddd, 4)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip2.linq new file mode 100644 index 000000000..b458a5d8f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip2.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, }; + +// Determine if sequence ends with the ends sequence +var result = seq1 + .EquiZip( + seq2, + (a, b) => new { Key = a, Value = b, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ Key = aaa, Value = 1 }, { Key = bb, Value = 2 }, { Key = c, Value = 3 }, { Key = ddd, Value = 4 }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip3.linq new file mode 100644 index 000000000..f9ead98dc --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip3.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, }; +var seq3 = new[] { 20, 5, 7, 12 }; + +// Determine if sequence ends with the ends sequence +var result = seq1 + .EquiZip( + seq2, + seq3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1, 20), (bb, 2, 5), (c, 3, 7), (ddd, 4, 12)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip4.linq new file mode 100644 index 000000000..40fc9b05f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip4.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, }; +var seq3 = new[] { 20, 5, 7, 12 }; + +// Determine if sequence ends with the ends sequence +var result = seq1 + .EquiZip( + seq2, + seq3, + (a, b, c) => new { A = a, B = b, C = c, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ A = aaa, B = 1, C = 20 }, { A = bb, B = 2, C = 5 }, { A = c, B = 3, C = 7 }, { A = ddd, B = 4, C = 12 }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip5.linq new file mode 100644 index 000000000..24bd54190 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip5.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, }; +var seq3 = new[] { 20, 5, 7, 12 }; +var seq4 = new[] { "zz", "yyyy", "xxx", "w", }; + +// Determine if sequence ends with the ends sequence +var result = seq1 + .EquiZip( + seq2, + seq3, + seq4); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1, 20, zz), (bb, 2, 5, yyyy), (c, 3, 7, xxx), (ddd, 4, 12, w)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip6.linq new file mode 100644 index 000000000..ce407d1dc --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/EquiZip/EquiZip6.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, }; +var seq3 = new[] { 20, 5, 7, 12 }; +var seq4 = new[] { "zz", "yyyy", "xxx", "w", }; + +// Determine if sequence ends with the ends sequence +var result = seq1 + .EquiZip( + seq2, + seq3, + seq4, + (a, b, c, d) => new + { + A = a, + B = b, + C = c, + D = d, + }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ A = aaa, B = 1, C = 20, D = zz }, { A = bb, B = 2, C = 5, D = yyyy }, { A = c, B = 3, C = 7, D = xxx }, { A = ddd, B = 4, C = 12, D = w }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Evaluate/Evaluate.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Evaluate/Evaluate.linq new file mode 100644 index 000000000..45743ecbc --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Evaluate/Evaluate.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +int Func1() => 3; +int Func2() => 5; +int Func3() => 1; + +// Execute an action for each element +var result = SuperEnumerable + .Evaluate(new[] { Func1, Func2, Func3 }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [3, 5, 1] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Exactly/Exactly.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Exactly/Exactly.linq new file mode 100644 index 000000000..93a4cebbb --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Exactly/Exactly.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +foreach (var x in Enumerable.Range(3, 5)) +{ + // Check that a sequence has an exact size + var result = sequence.Exactly(x); + + Console.WriteLine($"Exactly {x}: {result}"); +} + +// This code produces the following output: +// Exactly 3: False +// Exactly 4: False +// Exactly 5: True +// Exactly 6: False +// Exactly 7: False diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ExceptBy/ExceptBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ExceptBy/ExceptBy1.linq new file mode 100644 index 000000000..89f81f4fe --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ExceptBy/ExceptBy1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "aaa", "bb", "c", "dddd", }; + +// Determine if sequence ends with the ends sequence +var result = sequence.ExceptBy(new[] { "a", "b", }, s => s[0]); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [c, dddd] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ExceptBy/ExceptBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ExceptBy/ExceptBy2.linq new file mode 100644 index 000000000..d49f7719d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ExceptBy/ExceptBy2.linq @@ -0,0 +1,27 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "aaa", value: 1), + (key: "bb", value: 2), + (key: "c", value: 3), + (key: "dddd", value: 4), +}; + +// Determine if sequence ends with the ends sequence +var result = sequence + .ExceptBy( + new[] { (key: "A", value: 13), (key: "D", value: 14), }, + s => s.key[..1], + StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(bb, 2), (c, 3)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Exclude/Exclude.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Exclude/Exclude.linq new file mode 100644 index 000000000..56829d8da --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Exclude/Exclude.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Execute an action for each element +var result = sequence + .Exclude(3, 5); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 9, 10] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FallbackIfEmpty/FallbackIfEmpty1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FallbackIfEmpty/FallbackIfEmpty1.linq new file mode 100644 index 000000000..34a409d9d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FallbackIfEmpty/FallbackIfEmpty1.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var flag = false; +var sequence = SuperEnumerable.Defer( + () => + { + if (flag) return Enumerable.Empty(); + flag = true; + return Enumerable.Range(1, 5); + }); + +// Replace a sequence if it is empty. +var result = sequence.FallbackIfEmpty(10, 11, 12); + +Console.WriteLine( + "Non-Empty: [" + + string.Join(", ", result) + + "]"); + +Console.WriteLine( + "Empty: [" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// Non-Empty: [1, 2, 3, 4, 5] +// Empty: [10, 11, 12] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FallbackIfEmpty/FallbackIfEmpty2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FallbackIfEmpty/FallbackIfEmpty2.linq new file mode 100644 index 000000000..39bc99ef8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FallbackIfEmpty/FallbackIfEmpty2.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var flag = false; +var sequence = SuperEnumerable.Defer( + () => + { + if (flag) return Enumerable.Empty(); + flag = true; + return Enumerable.Range(1, 5); + }); + +// Replace a sequence if it is empty. +var result = sequence + .FallbackIfEmpty( + Enumerable.Range(20, 3)); + +Console.WriteLine( + "Non-Empty: [" + + string.Join(", ", result) + + "]"); + +Console.WriteLine( + "Empty: [" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// Non-Empty: [1, 2, 3, 4, 5] +// Empty: [20, 21, 22] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward1.linq new file mode 100644 index 000000000..8cbb5eca3 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new int?[] { null, null, 1, 2, null, null, null, 3, 4, null, null, }; + +// Fill in missing elements from later elements +var result = sequence.FillBackward(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 1, 1, 2, 3, 3, 3, 3, 4, , ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward2.linq new file mode 100644 index 000000000..6e11f824b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward2.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Fill in missing elements from later elements +var result = sequence + .FillBackward( + i => i % 3 < 2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [2, 2, 5, 5, 5, 8, 8, 8, 9, 10] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward3.linq new file mode 100644 index 000000000..fd50eb989 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillBackward/FillBackward3.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Fill in missing elements from later elements +var result = sequence + .FillBackward( + i => i % 3 < 2, + (cur, nxt) => cur * nxt * 100); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [200, 2, 1500, 2000, 5, 4800, 5600, 8, 9, 10] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward1.linq new file mode 100644 index 000000000..f43e26996 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new int?[] { null, null, 1, 2, null, null, null, 3, 4, null, null, }; + +// Fill in missing elements from previous elements +var result = sequence.FillForward(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [, , 1, 2, 2, 2, 2, 3, 4, 4, 4] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward2.linq new file mode 100644 index 000000000..305eeb28f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward2.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Fill in missing elements from previous elements +var result = sequence + .FillForward( + i => i % 3 < 2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 2, 2, 5, 5, 5, 8, 8, 8] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward3.linq new file mode 100644 index 000000000..d71e6fbfb --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FillForward/FillForward3.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Fill in missing elements from previous elements +var result = sequence + .FillForward( + i => i % 3 < 2, + (cur, nxt) => cur * nxt * 100); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 600, 800, 5, 3000, 3500, 8, 7200, 8000] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Finally/Finally.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Finally/Finally.linq new file mode 100644 index 000000000..a1eead01b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Finally/Finally.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Execute an action when enumeration is complete, regardless of success or fail. +var result = sequence + .Do(i => Console.Write($"{i}, ")) + .Finally(() => Console.WriteLine("Completed")); + +result.Consume(); + +// This code produces the following output: +// 1, 2, 3, 4, 5, Completed diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex1.linq new file mode 100644 index 000000000..05d0d9eaa --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex1.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 2, name: "Frank"), + (key: 3, name: "Jill"), + (key: 5, name: "Dave"), + (key: 8, name: "Jack"), + (key: 12, name: "Judith"), + (key: 14, name: "Robert"), + (key: 1, name: "Adam"), +}; + +// Find the first index that matches a condition +Console.WriteLine( + "'J' starts at index {0}", + sequence.FindIndex(x => x.name.StartsWith("J"))); + +Console.WriteLine( + "'Ju' starts at index {0}", + sequence.FindIndex(x => x.name.StartsWith("Ju"))); + +Console.WriteLine( + "'K' starts at index {0}", + sequence.FindIndex(x => x.name.StartsWith("K"))); + +// This code produces the following output: +// 'J' starts at index 1 +// 'Ju' starts at index 4 +// 'K' starts at index -1 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex2.linq new file mode 100644 index 000000000..ec0402c9d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex2.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 2, name: "Frank"), + (key: 3, name: "Jill"), + (key: 5, name: "Dave"), + (key: 8, name: "Jack"), + (key: 12, name: "Judith"), + (key: 14, name: "Robert"), + (key: 1, name: "Adam"), +}; + +// Find the first index that matches a condition +Console.WriteLine( + "'J' starts at index {0}, after index 4", + sequence.FindIndex( + x => x.name.StartsWith("J"), + 4)); + +Console.WriteLine( + "'J' starts at index {0}, after index ^2", + sequence.FindIndex( + x => x.name.StartsWith("J"), + ^2)); + +// This code produces the following output: +// 'J' starts at index 4, after index 4 +// 'J' starts at index -1, after index ^2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex3.linq new file mode 100644 index 000000000..ecc19e593 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindIndex/FindIndex3.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 2, name: "Frank"), + (key: 3, name: "Jill"), + (key: 5, name: "Dave"), + (key: 8, name: "Jack"), + (key: 12, name: "Judith"), + (key: 14, name: "Robert"), + (key: 1, name: "Adam"), +}; + +// Find the first index that matches a condition +Console.WriteLine( + "'J' starts at index {0}, after index 2-4", + sequence.FindIndex( + x => x.name.StartsWith("J"), + 2, + 3)); + +Console.WriteLine( + "'J' starts at index {0}, after index 2-2", + sequence.FindIndex( + x => x.name.StartsWith("J"), + 2, + 1)); + +// This code produces the following output: +// 'J' starts at index 3, after index 2-4 +// 'J' starts at index -1, after index 2-2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex1.linq new file mode 100644 index 000000000..ea6dbda61 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex1.linq @@ -0,0 +1,36 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 2, name: "Frank"), + (key: 3, name: "Jill"), + (key: 5, name: "Dave"), + (key: 8, name: "Jack"), + (key: 12, name: "Judith"), + (key: 14, name: "Robert"), + (key: 1, name: "Adam"), +}; + +// Find the first index that matches a condition +Console.WriteLine( + "'J' starts at index {0}", + sequence.FindLastIndex( + x => x.name.StartsWith("J"))); + +Console.WriteLine( + "'Ji' starts at index {0}", + sequence.FindLastIndex( + x => x.name.StartsWith("Ji"))); + +Console.WriteLine( + "'K' starts at index {0}", + sequence.FindLastIndex( + x => x.name.StartsWith("K"))); + +// This code produces the following output: +// 'J' starts at index 4 +// 'Ji' starts at index 1 +// 'K' starts at index -1 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex2.linq new file mode 100644 index 000000000..0a705c41c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex2.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 2, name: "Frank"), + (key: 3, name: "Jill"), + (key: 5, name: "Dave"), + (key: 8, name: "Jack"), + (key: 12, name: "Judith"), + (key: 14, name: "Robert"), + (key: 1, name: "Adam"), +}; + +// Find the first index that matches a condition +Console.WriteLine( + "'J' starts at index {0}, before index 0", + sequence.FindLastIndex( + x => x.name.StartsWith("J"), + 0)); + +Console.WriteLine( + "'J' starts at index {0}, before index ^2", + sequence.FindLastIndex( + x => x.name.StartsWith("J"), + ^2)); + +// This code produces the following output: +// 'J' starts at index -1, before index 0 +// 'J' starts at index 4, before index ^2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex3.linq new file mode 100644 index 000000000..8c1e23e38 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FindLastIndex/FindLastIndex3.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 2, name: "Frank"), + (key: 3, name: "Jill"), + (key: 5, name: "Dave"), + (key: 8, name: "Jack"), + (key: 12, name: "Judith"), + (key: 14, name: "Robert"), + (key: 1, name: "Adam"), +}; + +// Find the first index that matches a condition +Console.WriteLine( + "'J' starts at index {0}, before index 0", + sequence.FindLastIndex( + x => x.name.StartsWith("J"), + 1, + 1)); + +Console.WriteLine( + "'J' starts at index {0}, before index ^2", + sequence.FindLastIndex( + x => x.name.StartsWith("J"), + ^2)); + +// This code produces the following output: +// 'J' starts at index -1, before index 0 +// 'J' starts at index 4, before index ^2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten1.linq new file mode 100644 index 000000000..8dc301420 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten1.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = new object[] +{ + 1, 2, 3, + new object[] + { + 4, 5, + new object[] { 6, 7, }, + 8, 9, 10, + }, + 11, + new object[] + { + 12, 13, 14, + new object[] { 15, 16, 17, }, + 18, 19, + }, + 20, +}; + +// Flatten a hierarchical sequence +var result = sequence.Flatten(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten2.linq new file mode 100644 index 000000000..690860d3c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten2.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = new object[] +{ + 1, 2, 3, + new object[] + { + 4, 5, + new int[] { 6, 7, 8, }, + }, + 9, 10, 11, + new object[] + { + 12, 13, 14, + new object[] { 15, 16, 17, }, + 18, 19, + }, + 20, +}; + +// Flatten a hierarchical sequence +var result = sequence + .Flatten(e => e is not int[]); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, System.Int32[], 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten3.linq new file mode 100644 index 000000000..ab823bd89 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Flatten/Flatten3.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +var sequence = new object[] +{ + true, + false, + 1, + "bar", + new object[] + { + 2, + new[] + { + 3, + }, + }, + 'c', + 4, +}; + +// Flatten a hierarchical sequence +var result = sequence + .Flatten(obj => + obj switch + { + int => null, + IEnumerable inner => inner, + _ => Enumerable.Empty(), + }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold1.linq new file mode 100644 index 000000000..9667cfb07 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold1.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 1); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a) => a); + +Console.WriteLine(result); + +// This code produces the following output: +// 1 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold10.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold10.linq new file mode 100644 index 000000000..1128a9aea --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold10.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h, i, j) => + a + b + c + d + e + f + g + h + i + j); + +Console.WriteLine(result); + +// This code produces the following output: +// 55 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold11.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold11.linq new file mode 100644 index 000000000..32ff7d635 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold11.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 11); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h, i, j, k) => + a + b + c + d + e + f + g + h + i + j + k); + +Console.WriteLine(result); + +// This code produces the following output: +// 66 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold12.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold12.linq new file mode 100644 index 000000000..98f50c680 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold12.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 12); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h, i, j, k, l) => + a + b + c + d + e + f + g + h + i + j + k + l); + +Console.WriteLine(result); + +// This code produces the following output: +// 78 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold13.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold13.linq new file mode 100644 index 000000000..f6b8ed59b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold13.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 13); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h, i, j, k, l, m) => + a + b + c + d + e + f + g + h + i + j + k + l + m); + +Console.WriteLine(result); + +// This code produces the following output: +// 91 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold14.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold14.linq new file mode 100644 index 000000000..b8ae6e340 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold14.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 14); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n) => + a + b + c + d + e + f + g + h + i + j + k + l + m + n); + +Console.WriteLine(result); + +// This code produces the following output: +// 105 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold15.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold15.linq new file mode 100644 index 000000000..88485c56f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold15.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 15); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) => + a + b + c + d + e + f + g + h + i + j + k + l + m + n + o); + +Console.WriteLine(result); + +// This code produces the following output: +// 120 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold16.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold16.linq new file mode 100644 index 000000000..4237bd173 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold16.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 16); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) => + a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p); + +Console.WriteLine(result); + +// This code produces the following output: +// 136 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold2.linq new file mode 100644 index 000000000..1bb4c3b95 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold2.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 2); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b) => a + b); + +Console.WriteLine(result); + +// This code produces the following output: +// 3 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold3.linq new file mode 100644 index 000000000..6e3f671ca --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold3.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c) => a + b + c); + +Console.WriteLine(result); + +// This code produces the following output: +// 6 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold4.linq new file mode 100644 index 000000000..e40ec88f0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold4.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 4); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d) => a + b + c + d); + +Console.WriteLine(result); + +// This code produces the following output: +// 10 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold5.linq new file mode 100644 index 000000000..e8d4d9c1a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold5.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e) => a + b + c + d + e); + +Console.WriteLine(result); + +// This code produces the following output: +// 15 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold6.linq new file mode 100644 index 000000000..61fc9fb8e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold6.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 6); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f) => a + b + c + d + e + f); + +Console.WriteLine(result); + +// This code produces the following output: +// 21 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold7.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold7.linq new file mode 100644 index 000000000..b2a4d41ee --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold7.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 7); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g) => + a + b + c + d + e + f + g); + +Console.WriteLine(result); + +// This code produces the following output: +// 28 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold8.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold8.linq new file mode 100644 index 000000000..0ff67cd11 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold8.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 8); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h) => + a + b + c + d + e + f + g + h); + +Console.WriteLine(result); + +// This code produces the following output: +// 36 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold9.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold9.linq new file mode 100644 index 000000000..1d45fb6e6 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Fold/Fold9.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 9); + +// Fold a sequence into a single value. +var result = sequence + .Fold((a, b, c, d, e, f, g, h, i) => + a + b + c + d + e + f + g + h + i); + +Console.WriteLine(result); + +// This code produces the following output: +// 45 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ForEach/ForEach1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ForEach/ForEach1.linq new file mode 100644 index 000000000..935f4c098 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ForEach/ForEach1.linq @@ -0,0 +1,13 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Fold a sequence into a single value. +sequence + .ForEach(x => Console.Write($"{x}, ")); + +// This code produces the following output: +// 1, 2, 3, 4, 5, diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ForEach/ForEach2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ForEach/ForEach2.linq new file mode 100644 index 000000000..ed5f638b3 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ForEach/ForEach2.linq @@ -0,0 +1,13 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Fold a sequence into a single value. +sequence + .ForEach((x, i) => Console.Write($"({x}, {i}), ")); + +// This code produces the following output: +// (1, 0), (2, 1), (3, 2), (4, 3), (5, 4), diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From1.linq new file mode 100644 index 000000000..49f717337 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +int Func1() => 3; + +// Execute an action for each element +var result = SuperEnumerable + .From(Func1); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From2.linq new file mode 100644 index 000000000..eeeb87bec --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From2.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +int Func1() => 3; +int Func2() => 5; + +// Execute an action for each element +var result = SuperEnumerable + .From(Func1, Func2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [3, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From3.linq new file mode 100644 index 000000000..5861dbb67 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From3.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +int Func1() => 3; +int Func2() => 5; +int Func3() => 1; + +// Execute an action for each element +var result = SuperEnumerable + .From(Func1, Func2, Func3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [3, 5, 1] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From4.linq new file mode 100644 index 000000000..8e32764b4 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/From/From4.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +int Func1() => 3; +int Func2() => 5; +int Func3() => 1; +int Func4() => 7; + +// Execute an action for each element +var result = SuperEnumerable + .From(Func1, Func2, Func3, Func4); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [3, 5, 1, 7] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin1.linq new file mode 100644 index 000000000..283747974 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin1.linq @@ -0,0 +1,46 @@ + + SuperLinq + SuperLinq + + +var people = new Person[] +{ + new("John Doe", 1), + new("Jane Doe", 1), + new("Lucy Ricardo", 2), + new("Ricky Ricardo", 2), + new("Fred Mertz", 3), + new("Ethel Mertz", 3), +}; + +var pets = new Pet[] +{ + new("Bear", 3), + new("Polly", 2), + new("Minnie", 2), + new("Mittens", 1), + new("Patches", 1), + new("Paws", 1), +}; + +var results = people + .FullGroupJoin( + pets, + p => p.FamilyId, + p => p.FamilyId); + +foreach (var (familyId, familyPeople, familyPets) in results) +{ + var str1 = string.Join(", ", familyPeople.Select(p => p.Name)); + var str2 = string.Join(", ", familyPets.Select(p => p.Name)); + Console.WriteLine($"({familyId}, [{str1}], [{str2}])"); +} + +// This code produces the following output: +// (1, [John Doe, Jane Doe], [Mittens, Patches, Paws]) +// (2, [Lucy Ricardo, Ricky Ricardo], [Polly, Minnie]) +// (3, [Fred Mertz, Ethel Mertz], [Bear]) + + +record Person(string Name, int FamilyId); +record Pet(string Name, int FamilyId); diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin2.linq new file mode 100644 index 000000000..a5dfe2c18 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin2.linq @@ -0,0 +1,57 @@ + + SuperLinq + SuperLinq + System.Diagnostics.CodeAnalysis + + +var people = new Person[] +{ + new("John Doe", 1), + new("Jane Doe", 1), + new("Lucy Ricardo", 2), + new("Ricky Ricardo", 2), + new("Fred Mertz", 3), + new("Ethel Mertz", 3), +}; + +var pets = new Pet[] +{ + new("Bear", 103), + new("Polly", 102), + new("Minnie", 102), + new("Mittens", 101), + new("Patches", 101), + new("Paws", 101), +}; + +var results = people + .FullGroupJoin( + pets, + p => p.FamilyId, + p => p.FamilyId, + new IntBy100Comparer()); + +foreach (var (familyId, familyPeople, familyPets) in results) +{ + var str1 = string.Join(", ", familyPeople.Select(p => p.Name)); + var str2 = string.Join(", ", familyPets.Select(p => p.Name)); + Console.WriteLine($"({familyId}, [{str1}], [{str2}])"); +} + +// This code produces the following output: +// (1, [John Doe, Jane Doe], [Mittens, Patches, Paws]) +// (2, [Lucy Ricardo, Ricky Ricardo], [Polly, Minnie]) +// (3, [Fred Mertz, Ethel Mertz], [Bear]) + + +record Person(string Name, int FamilyId); +record Pet(string Name, int FamilyId); + +class IntBy100Comparer : IEqualityComparer +{ + public bool Equals(int x, int y) => + x % 100 == y % 100; + + public int GetHashCode(int obj) => + (obj % 100).GetHashCode(); +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin3.linq new file mode 100644 index 000000000..7f4444ebe --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin3.linq @@ -0,0 +1,48 @@ + + SuperLinq + SuperLinq + + +var people = new Person[] +{ + new("John Doe", 1), + new("Jane Doe", 1), + new("Lucy Ricardo", 2), + new("Ricky Ricardo", 2), + new("Fred Mertz", 3), + new("Ethel Mertz", 3), +}; + +var pets = new Pet[] +{ + new("Bear", 3), + new("Polly", 2), + new("Minnie", 2), + new("Mittens", 1), + new("Patches", 1), + new("Paws", 1), +}; + +var results = people + .FullGroupJoin( + pets, + p => p.FamilyId, + p => p.FamilyId, + (familyId, familyPeople, familyPets) => + { + var str1 = string.Join(", ", familyPeople.Select(p => p.Name)); + var str2 = string.Join(", ", familyPets.Select(p => p.Name)); + return $"({familyId}, [{str1}], [{str2}])"; + }); + +foreach (var str in results) + Console.WriteLine(str); + +// This code produces the following output: +// (1, [John Doe, Jane Doe], [Mittens, Patches, Paws]) +// (2, [Lucy Ricardo, Ricky Ricardo], [Polly, Minnie]) +// (3, [Fred Mertz, Ethel Mertz], [Bear]) + + +record Person(string Name, int FamilyId); +record Pet(string Name, int FamilyId); diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin4.linq new file mode 100644 index 000000000..65e30365c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/FullGroupJoin/FullGroupJoin4.linq @@ -0,0 +1,58 @@ + + SuperLinq + SuperLinq + System.Diagnostics.CodeAnalysis + + +var people = new Person[] +{ + new("John Doe", 1), + new("Jane Doe", 1), + new("Lucy Ricardo", 2), + new("Ricky Ricardo", 2), + new("Fred Mertz", 3), + new("Ethel Mertz", 3), +}; + +var pets = new Pet[] +{ + new("Bear", 103), + new("Polly", 102), + new("Minnie", 102), + new("Mittens", 101), + new("Patches", 101), + new("Paws", 101), +}; + +var results = people + .FullGroupJoin( + pets, + p => p.FamilyId, + p => p.FamilyId, + (familyId, familyPeople, familyPets) => + { + var str1 = string.Join(", ", familyPeople.Select(p => p.Name)); + var str2 = string.Join(", ", familyPets.Select(p => p.Name)); + return $"({familyId}, [{str1}], [{str2}])"; + }, + new IntBy100Comparer()); + +foreach (var str in results) + Console.WriteLine(str); + +// This code produces the following output: +// (1, [John Doe, Jane Doe], [Mittens, Patches, Paws]) +// (2, [Lucy Ricardo, Ricky Ricardo], [Polly, Minnie]) +// (3, [Fred Mertz, Ethel Mertz], [Bear]) + +record Person(string Name, int FamilyId); +record Pet(string Name, int FamilyId); + +class IntBy100Comparer : IEqualityComparer +{ + public bool Equals(int x, int y) => + x % 100 == y % 100; + + public int GetHashCode(int obj) => + (obj % 100).GetHashCode(); +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Generate/Generate.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Generate/Generate.linq new file mode 100644 index 000000000..4dc731695 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Generate/Generate.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +// Generate a sequence using a generator function +var result = SuperEnumerable + .Generate(1, n => n * 2) + .Take(10); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 4, 8, 16, 32, 64, 128, 256, 512] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath1.linq new file mode 100644 index 000000000..d774f43f8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath1.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var start = (x: 0, y: 0); +var end = (x: 2, y: 2); +((int x, int y) p, double cost, double bestGuess) GetNeighbor((int x, int y) p, double newCost) +{ + var xD = p.x - end.x; + var yD = p.y - end.y; + var dist = Math.Sqrt((xD * xD) + (yD * yD)); + return (p, newCost, newCost + dist); +} + +IEnumerable<((int x, int y) p, double cost, double bestGuess)> GetNeighbors((int x, int y) p, double cost) +{ + yield return GetNeighbor((p.x + 1, p.y), cost + 1.001d); + yield return GetNeighbor((p.x, p.y + 1), cost + 1.002d); + yield return GetNeighbor((p.x - 1, p.y), cost + 1.003d); + yield return GetNeighbor((p.x, p.y - 1), cost + 1.004d); +} + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPath<(int x, int y), double>( + start, + GetNeighbors, + end); + +Console.WriteLine(string.Join(" -> ", result.Select(x => $"({x.nextState}, {x.cost:N3})"))); + +// This code produces the following output: +// ((0, 0), 0.000) -> ((1, 0), 1.001) -> ((2, 0), 2.002) -> ((2, 1), 3.004) -> ((2, 2), 4.006) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath2.linq new file mode 100644 index 000000000..0c4bec613 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath2.linq @@ -0,0 +1,48 @@ + + SuperLinq + SuperLinq + + +var start = (x: 0, y: 0); +var end = (x: -2, y: -2); +((int x, int y) p, double cost, double bestGuess) GetNeighbor((int x, int y) p, double newCost) +{ + var xD = p.x - end.x; + var yD = p.y - end.y; + var dist = Math.Sqrt((xD * xD) + (yD * yD)); + return (p, newCost, newCost + dist); +} + +IEnumerable<((int x, int y) p, double cost, double bestGuess)> GetNeighbors((int x, int y) p, double cost) +{ + yield return GetNeighbor((p.x + 1, p.y), cost + 1.001d); + yield return GetNeighbor((p.x, p.y + 1), cost + 1.002d); + yield return GetNeighbor((p.x - 1, p.y), cost + 1.003d); + yield return GetNeighbor((p.x, p.y - 1), cost + 1.004d); +} + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPath<(int x, int y), double>( + start, + GetNeighbors, + end, + new PointComparer(), + null); + +Console.WriteLine(string.Join(" -> ", result.Select(x => $"({x.nextState}, {x.cost:N3})"))); + +// This code produces the following output: +// ((0, 0), 0.000) -> ((-1, 0), 1.003) -> ((-1, -1), 2.007) -> ((-2, -1), 3.010) -> ((-2, -2), 4.014) + +class PointComparer : IEqualityComparer<(int x, int y)> +{ + public bool Equals((int x, int y) x, (int x, int y) y) => + ManhattanDistance(x) == ManhattanDistance(y); + + public int GetHashCode((int x, int y) obj) => + ManhattanDistance(obj).GetHashCode(); + + private static double ManhattanDistance((int x, int y) obj) => + Math.Sqrt((obj.x * obj.x) + (obj.y * obj.y)); +} \ No newline at end of file diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath3.linq new file mode 100644 index 000000000..c433c00d1 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath3.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var start = (x: 0, y: 0); +var end = (x: 2, y: 2); +((int x, int y) p, double cost, double bestGuess) GetNeighbor((int x, int y) p, double newCost) +{ + var xD = p.x - end.x; + var yD = p.y - end.y; + var dist = Math.Sqrt((xD * xD) + (yD * yD)); + return (p, newCost, newCost + dist); +} + +IEnumerable<((int x, int y) p, double cost, double bestGuess)> GetNeighbors((int x, int y) p, double cost) +{ + yield return GetNeighbor((p.x + 1, p.y), cost + 1.001d); + yield return GetNeighbor((p.x, p.y + 1), cost + 1.002d); + yield return GetNeighbor((p.x - 1, p.y), cost + 1.003d); + yield return GetNeighbor((p.x, p.y - 1), cost + 1.004d); +} + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPath<(int x, int y), double>( + start, + GetNeighbors, + state => state == end); + +Console.WriteLine(string.Join(" -> ", result.Select(x => $"({x.nextState}, {x.cost:N3})"))); + +// This code produces the following output: +// ((0, 0), 0.000) -> ((1, 0), 1.001) -> ((2, 0), 2.002) -> ((2, 1), 3.004) -> ((2, 2), 4.006) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath4.linq new file mode 100644 index 000000000..f56150152 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.AStar/GetShortestPath4.linq @@ -0,0 +1,48 @@ + + SuperLinq + SuperLinq + + +var start = (x: 0, y: 0); +var end = (x: -2, y: -2); +((int x, int y) p, double cost, double bestGuess) GetNeighbor((int x, int y) p, double newCost) +{ + var xD = p.x - end.x; + var yD = p.y - end.y; + var dist = Math.Sqrt((xD * xD) + (yD * yD)); + return (p, newCost, newCost + dist); +} + +IEnumerable<((int x, int y) p, double cost, double bestGuess)> GetNeighbors((int x, int y) p, double cost) +{ + yield return GetNeighbor((p.x + 1, p.y), cost + 1.001d); + yield return GetNeighbor((p.x, p.y + 1), cost + 1.002d); + yield return GetNeighbor((p.x - 1, p.y), cost + 1.003d); + yield return GetNeighbor((p.x, p.y - 1), cost + 1.004d); +} + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPath<(int x, int y), double>( + start, + GetNeighbors, + state => new PointComparer().Equals(state, end), + new PointComparer(), + null); + +Console.WriteLine(string.Join(" -> ", result.Select(x => $"({x.nextState}, {x.cost:N3})"))); + +// This code produces the following output: +// ((0, 0), 0.000) -> ((-1, 0), 1.003) -> ((-1, -1), 2.007) -> ((-2, -1), 3.010) -> ((-2, -2), 4.014) + +class PointComparer : IEqualityComparer<(int x, int y)> +{ + public bool Equals((int x, int y) x, (int x, int y) y) => + ManhattanDistance(x) == ManhattanDistance(y); + + public int GetHashCode((int x, int y) obj) => + ManhattanDistance(obj).GetHashCode(); + + private static double ManhattanDistance((int x, int y) obj) => + Math.Sqrt((obj.x * obj.x) + (obj.y * obj.y)); +} \ No newline at end of file diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath1.linq new file mode 100644 index 000000000..89bee0032 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath1.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: "start", to: "a", cost: 1), + (from: "a", to: "b", cost: 2), + (from: "b", to: "c", cost: 3), + (from: "c", to: "d", cost: 4), + (from: "d", to: "end", cost: 5), + (from: "start", to: "A", cost: 10), + (from: "A", to: "B", cost: 20), + (from: "B", to: "C", cost: 30), + (from: "C", to: "D", cost: 40), + (from: "D", to: "end", cost: 50), + (from: "start", to: "END", cost: 10), + (from: "start", to: "END", cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to != "start" + && x.from != "end") + .ToLookup(x => x.from, x => (x.to, x.cost)); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPath( + "start", + (state, cost) => map[state] + .Select(x => (x.to, x.cost + cost)), + "end"); + +Console.WriteLine(string.Join(" -> ", result)); + +// This code produces the following output: +// (start, 0) -> (a, 1) -> (b, 3) -> (c, 6) -> (d, 10) -> (end, 15) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath2.linq new file mode 100644 index 000000000..d7b340cc7 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath2.linq @@ -0,0 +1,42 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: "start", to: "a", cost: 1), + (from: "a", to: "b", cost: 2), + (from: "b", to: "c", cost: 3), + (from: "c", to: "d", cost: 4), + (from: "d", to: "end", cost: 5), + (from: "start", to: "A", cost: 10), + (from: "A", to: "B", cost: 20), + (from: "B", to: "C", cost: 30), + (from: "C", to: "D", cost: 40), + (from: "D", to: "end", cost: 50), + (from: "start", to: "END", cost: 10), + (from: "start", to: "END", cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to != "start" + && x.from != "end") + .ToLookup(x => x.from, x => (x.to, x.cost), StringComparer.OrdinalIgnoreCase); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPath( + "start", + (state, cost) => map[state] + .Select(x => (x.to, x.cost + cost)), + "end", + StringComparer.OrdinalIgnoreCase, + default); + +Console.WriteLine(string.Join(" -> ", result)); + +// This code produces the following output: +// (start, 0) -> (END, 10) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath3.linq new file mode 100644 index 000000000..48380ad93 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath3.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: (id: "start", index: 1), to: (id: "a", index: 2), cost: 1), + (from: (id: "a", index: 2), to: (id: "b", index: 3), cost: 2), + (from: (id: "b", index: 3), to: (id: "c", index: 3), cost: 3), + (from: (id: "c", index: 3), to: (id: "d", index: 4), cost: 4), + (from: (id: "d", index: 4), to: (id: "end", index: 5), cost: 5), + (from: (id: "start", index: 1), to: (id: "A", index: 6), cost: 10), + (from: (id: "A", index: 6), to: (id: "B", index: 7), cost: 20), + (from: (id: "B", index: 7), to: (id: "C", index: 8), cost: 30), + (from: (id: "C", index: 8), to: (id: "D", index: 9), cost: 40), + (from: (id: "D", index: 9), to: (id: "end", index: 5), cost: 50), + (from: (id: "start", index: 1), to: (id: "END", index: 10), cost: 10), + (from: (id: "start", index: 1), to: (id: "END", index: 10), cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to.id != "start" + && x.from.id != "end") + .ToLookup(x => x.from.id, x => (x.to, x.cost)); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPath<(string id, int index), int>( + ("start", 1), + (state, cost) => map[state.id] + .Select(x => (x.to, x.cost + cost)), + x => x.id.Equals("end", StringComparison.OrdinalIgnoreCase)); + +Console.WriteLine(string.Join(" -> ", result)); + +// This code produces the following output: +// ((start, 1), 0) -> ((a, 2), 1) -> ((b, 3), 3) -> ((c, 3), 6) -> ((d, 4), 10) -> ((end, 5), 15) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath4.linq new file mode 100644 index 000000000..304a56cf1 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPath.Dijkstra/GetShortestPath4.linq @@ -0,0 +1,51 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: (id: "start", index: 1), to: (id: "a", index: 2), cost: 1), + (from: (id: "a", index: 2), to: (id: "b", index: 3), cost: 2), + (from: (id: "b", index: 3), to: (id: "c", index: 3), cost: 3), + (from: (id: "c", index: 3), to: (id: "d", index: 4), cost: 4), + (from: (id: "d", index: 4), to: (id: "end", index: 5), cost: 5), + (from: (id: "start", index: 1), to: (id: "A", index: 6), cost: 10), + (from: (id: "A", index: 6), to: (id: "B", index: 7), cost: 20), + (from: (id: "B", index: 7), to: (id: "C", index: 8), cost: 30), + (from: (id: "C", index: 8), to: (id: "D", index: 9), cost: 40), + (from: (id: "D", index: 9), to: (id: "end", index: 5), cost: 50), + (from: (id: "start", index: 1), to: (id: "END", index: 10), cost: 10), + (from: (id: "start", index: 1), to: (id: "END", index: 10), cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to.id != "start" + && x.from.id != "end") + .ToLookup(x => x.from.id, x => (x.to, x.cost), StringComparer.OrdinalIgnoreCase); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPath<(string id, int index), int>( + ("start", 1), + (state, cost) => map[state.id] + .Select(x => (x.to, x.cost + cost)), + x => x.id.Equals("end", StringComparison.OrdinalIgnoreCase), + new StateComparer(), + default); + +Console.WriteLine(string.Join(" -> ", result)); + +// This code produces the following output: +// ((start, 1), 0) -> ((END, 10), 10) + +class StateComparer : IEqualityComparer<(string id, int index)> +{ + public bool Equals((string id, int index) x, (string id, int index) y) => + StringComparer.OrdinalIgnoreCase.Equals(x.id, y.id); + + public int GetHashCode((string id, int index) obj) => + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.id); +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost1.linq new file mode 100644 index 000000000..c92447f0d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost1.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var start = (x: 0, y: 0); +var end = (x: 2, y: 2); +((int x, int y) p, double cost, double bestGuess) GetNeighbor((int x, int y) p, double newCost) +{ + var xD = p.x - end.x; + var yD = p.y - end.y; + var dist = Math.Sqrt((xD * xD) + (yD * yD)); + return (p, newCost, newCost + dist); +} + +IEnumerable<((int x, int y) p, double cost, double bestGuess)> GetNeighbors((int x, int y) p, double cost) +{ + yield return GetNeighbor((p.x + 1, p.y), cost + 1.001d); + yield return GetNeighbor((p.x, p.y + 1), cost + 1.002d); + yield return GetNeighbor((p.x - 1, p.y), cost + 1.003d); + yield return GetNeighbor((p.x, p.y - 1), cost + 1.004d); +} + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPathCost<(int x, int y), double>( + start, + GetNeighbors, + end); + +Console.WriteLine($"cost: {result:N3}"); + +// This code produces the following output: +// cost: 4.006 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost2.linq new file mode 100644 index 000000000..85b5cdc6f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost2.linq @@ -0,0 +1,48 @@ + + SuperLinq + SuperLinq + + +var start = (x: 0, y: 0); +var end = (x: -2, y: -2); +((int x, int y) p, double cost, double bestGuess) GetNeighbor((int x, int y) p, double newCost) +{ + var xD = p.x - end.x; + var yD = p.y - end.y; + var dist = Math.Sqrt((xD * xD) + (yD * yD)); + return (p, newCost, newCost + dist); +} + +IEnumerable<((int x, int y) p, double cost, double bestGuess)> GetNeighbors((int x, int y) p, double cost) +{ + yield return GetNeighbor((p.x + 1, p.y), cost + 1.001d); + yield return GetNeighbor((p.x, p.y + 1), cost + 1.002d); + yield return GetNeighbor((p.x - 1, p.y), cost + 1.003d); + yield return GetNeighbor((p.x, p.y - 1), cost + 1.004d); +} + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPathCost<(int x, int y), double>( + start, + GetNeighbors, + end, + new PointComparer(), + null); + +Console.WriteLine($"cost: {result:N3}"); + +// This code produces the following output: +// cost: 4.014 + +class PointComparer : IEqualityComparer<(int x, int y)> +{ + public bool Equals((int x, int y) x, (int x, int y) y) => + ManhattanDistance(x) == ManhattanDistance(y); + + public int GetHashCode((int x, int y) obj) => + ManhattanDistance(obj).GetHashCode(); + + private static double ManhattanDistance((int x, int y) obj) => + Math.Sqrt((obj.x * obj.x) + (obj.y * obj.y)); +} \ No newline at end of file diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost3.linq new file mode 100644 index 000000000..ba0e7df54 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost3.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var start = (x: 0, y: 0); +var end = (x: 2, y: 2); +((int x, int y) p, double cost, double bestGuess) GetNeighbor((int x, int y) p, double newCost) +{ + var xD = p.x - end.x; + var yD = p.y - end.y; + var dist = Math.Sqrt((xD * xD) + (yD * yD)); + return (p, newCost, newCost + dist); +} + +IEnumerable<((int x, int y) p, double cost, double bestGuess)> GetNeighbors((int x, int y) p, double cost) +{ + yield return GetNeighbor((p.x + 1, p.y), cost + 1.001d); + yield return GetNeighbor((p.x, p.y + 1), cost + 1.002d); + yield return GetNeighbor((p.x - 1, p.y), cost + 1.003d); + yield return GetNeighbor((p.x, p.y - 1), cost + 1.004d); +} + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPathCost<(int x, int y), double>( + start, + GetNeighbors, + state => state == end); + +Console.WriteLine($"cost: {result:N3}"); + +// This code produces the following output: +// cost: 4.006 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost4.linq new file mode 100644 index 000000000..57186be79 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.AStar/GetShortestPathCost4.linq @@ -0,0 +1,48 @@ + + SuperLinq + SuperLinq + + +var start = (x: 0, y: 0); +var end = (x: -2, y: -2); +((int x, int y) p, double cost, double bestGuess) GetNeighbor((int x, int y) p, double newCost) +{ + var xD = p.x - end.x; + var yD = p.y - end.y; + var dist = Math.Sqrt((xD * xD) + (yD * yD)); + return (p, newCost, newCost + dist); +} + +IEnumerable<((int x, int y) p, double cost, double bestGuess)> GetNeighbors((int x, int y) p, double cost) +{ + yield return GetNeighbor((p.x + 1, p.y), cost + 1.001d); + yield return GetNeighbor((p.x, p.y + 1), cost + 1.002d); + yield return GetNeighbor((p.x - 1, p.y), cost + 1.003d); + yield return GetNeighbor((p.x, p.y - 1), cost + 1.004d); +} + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPathCost<(int x, int y), double>( + start, + GetNeighbors, + state => new PointComparer().Equals(state, end), + new PointComparer(), + null); + +Console.WriteLine($"cost: {result:N3}"); + +// This code produces the following output: +// cost: 4.014 + +class PointComparer : IEqualityComparer<(int x, int y)> +{ + public bool Equals((int x, int y) x, (int x, int y) y) => + ManhattanDistance(x) == ManhattanDistance(y); + + public int GetHashCode((int x, int y) obj) => + ManhattanDistance(obj).GetHashCode(); + + private static double ManhattanDistance((int x, int y) obj) => + Math.Sqrt((obj.x * obj.x) + (obj.y * obj.y)); +} \ No newline at end of file diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost1.linq new file mode 100644 index 000000000..205082ef5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost1.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: "start", to: "a", cost: 1), + (from: "a", to: "b", cost: 2), + (from: "b", to: "c", cost: 3), + (from: "c", to: "d", cost: 4), + (from: "d", to: "end", cost: 5), + (from: "start", to: "A", cost: 10), + (from: "A", to: "B", cost: 20), + (from: "B", to: "C", cost: 30), + (from: "C", to: "D", cost: 40), + (from: "D", to: "end", cost: 50), + (from: "start", to: "END", cost: 10), + (from: "start", to: "END", cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to != "start" + && x.from != "end") + .ToLookup(x => x.from, x => (x.to, x.cost)); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPathCost( + "start", + (state, cost) => map[state] + .Select(x => (x.to, x.cost + cost)), + "end"); + +Console.WriteLine($"cost: {result}"); + +// This code produces the following output: +// cost: 15 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost2.linq new file mode 100644 index 000000000..ff2f5b194 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost2.linq @@ -0,0 +1,42 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: "start", to: "a", cost: 1), + (from: "a", to: "b", cost: 2), + (from: "b", to: "c", cost: 3), + (from: "c", to: "d", cost: 4), + (from: "d", to: "end", cost: 5), + (from: "start", to: "A", cost: 10), + (from: "A", to: "B", cost: 20), + (from: "B", to: "C", cost: 30), + (from: "C", to: "D", cost: 40), + (from: "D", to: "end", cost: 50), + (from: "start", to: "END", cost: 10), + (from: "start", to: "END", cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to != "start" + && x.from != "end") + .ToLookup(x => x.from, x => (x.to, x.cost), StringComparer.OrdinalIgnoreCase); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPathCost( + "start", + (state, cost) => map[state] + .Select(x => (x.to, x.cost + cost)), + "end", + StringComparer.OrdinalIgnoreCase, + default); + +Console.WriteLine($"cost: {result}"); + +// This code produces the following output: +// cost: 10 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost3.linq new file mode 100644 index 000000000..7b063a4e7 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost3.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: (id: "start", index: 1), to: (id: "a", index: 2), cost: 1), + (from: (id: "a", index: 2), to: (id: "b", index: 3), cost: 2), + (from: (id: "b", index: 3), to: (id: "c", index: 3), cost: 3), + (from: (id: "c", index: 3), to: (id: "d", index: 4), cost: 4), + (from: (id: "d", index: 4), to: (id: "end", index: 5), cost: 5), + (from: (id: "start", index: 1), to: (id: "A", index: 6), cost: 10), + (from: (id: "A", index: 6), to: (id: "B", index: 7), cost: 20), + (from: (id: "B", index: 7), to: (id: "C", index: 8), cost: 30), + (from: (id: "C", index: 8), to: (id: "D", index: 9), cost: 40), + (from: (id: "D", index: 9), to: (id: "end", index: 5), cost: 50), + (from: (id: "start", index: 1), to: (id: "END", index: 10), cost: 10), + (from: (id: "start", index: 1), to: (id: "END", index: 10), cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to.id != "start" + && x.from.id != "end") + .ToLookup(x => x.from.id, x => (x.to, x.cost)); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPathCost<(string id, int index), int>( + ("start", 1), + (state, cost) => map[state.id] + .Select(x => (x.to, x.cost + cost)), + x => x.id == "end"); + +Console.WriteLine($"cost: {result}"); + +// This code produces the following output: +// cost: 15 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost4.linq new file mode 100644 index 000000000..6da8b15b7 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPathCost.Dijkstra/GetShortestPathCost4.linq @@ -0,0 +1,51 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: (id: "start", index: 1), to: (id: "a", index: 2), cost: 1), + (from: (id: "a", index: 2), to: (id: "b", index: 3), cost: 2), + (from: (id: "b", index: 3), to: (id: "c", index: 3), cost: 3), + (from: (id: "c", index: 3), to: (id: "d", index: 4), cost: 4), + (from: (id: "d", index: 4), to: (id: "end", index: 5), cost: 5), + (from: (id: "start", index: 1), to: (id: "A", index: 6), cost: 10), + (from: (id: "A", index: 6), to: (id: "B", index: 7), cost: 20), + (from: (id: "B", index: 7), to: (id: "C", index: 8), cost: 30), + (from: (id: "C", index: 8), to: (id: "D", index: 9), cost: 40), + (from: (id: "D", index: 9), to: (id: "end", index: 5), cost: 50), + (from: (id: "start", index: 1), to: (id: "END", index: 10), cost: 10), + (from: (id: "start", index: 1), to: (id: "END", index: 10), cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to.id != "start" + && x.from.id != "end") + .ToLookup(x => x.from.id, x => (x.to, x.cost), StringComparer.OrdinalIgnoreCase); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPathCost<(string id, int index), int>( + ("start", 1), + (state, cost) => map[state.id] + .Select(x => (x.to, x.cost + cost)), + x => x.id.Equals("end", StringComparison.OrdinalIgnoreCase), + new StateComparer(), + default); + +Console.WriteLine($"cost: {result}"); + +// This code produces the following output: +// cost: 10 + +class StateComparer : IEqualityComparer<(string id, int index)> +{ + public bool Equals((string id, int index) x, (string id, int index) y) => + StringComparer.OrdinalIgnoreCase.Equals(x.id, y.id); + + public int GetHashCode((string id, int index) obj) => + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.id); +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPaths/GetShortestPaths1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPaths/GetShortestPaths1.linq new file mode 100644 index 000000000..173effa17 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPaths/GetShortestPaths1.linq @@ -0,0 +1,52 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: "start", to: "a", cost: 1), + (from: "a", to: "b", cost: 2), + (from: "b", to: "c", cost: 3), + (from: "c", to: "d", cost: 4), + (from: "d", to: "end", cost: 5), + (from: "start", to: "A", cost: 10), + (from: "A", to: "B", cost: 20), + (from: "B", to: "C", cost: 30), + (from: "C", to: "D", cost: 40), + (from: "D", to: "end", cost: 50), + (from: "start", to: "END", cost: 10), + (from: "start", to: "END", cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to != "start" + && x.from != "end") + .ToLookup(x => x.from, x => (x.to, x.cost)); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPaths( + "start", + (state, cost) => map[state] + .Select(x => (x.to, x.cost + cost))); + +foreach (var (key, (from, cost)) in result) +{ + Console.WriteLine($"[{key}] = (from: {from}, totalCost: {cost})"); +} + +// This code produces the following output: +// [start] = (from: , totalCost: 0) +// [a] = (from: start, totalCost: 1) +// [b] = (from: a, totalCost: 3) +// [c] = (from: b, totalCost: 6) +// [END] = (from: start, totalCost: 10) +// [d] = (from: c, totalCost: 10) +// [A] = (from: start, totalCost: 10) +// [end] = (from: d, totalCost: 15) +// [B] = (from: A, totalCost: 30) +// [C] = (from: B, totalCost: 60) +// [D] = (from: C, totalCost: 100) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPaths/GetShortestPaths2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPaths/GetShortestPaths2.linq new file mode 100644 index 000000000..90a30dbfe --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPaths/GetShortestPaths2.linq @@ -0,0 +1,49 @@ + + SuperLinq + SuperLinq + + +var costs = + new[] + { + (from: "start", to: "a", cost: 1), + (from: "a", to: "b", cost: 2), + (from: "b", to: "c", cost: 3), + (from: "c", to: "d", cost: 4), + (from: "d", to: "end", cost: 5), + (from: "start", to: "A", cost: 10), + (from: "A", to: "B", cost: 20), + (from: "B", to: "C", cost: 30), + (from: "C", to: "D", cost: 40), + (from: "D", to: "end", cost: 50), + (from: "start", to: "END", cost: 10), + (from: "start", to: "END", cost: 1000), + }; +var map = costs + .Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) + .Where(x => + x.to != "start" + && x.from != "end") + .ToLookup(x => x.from, x => (x.to, x.cost)); + +// Find the shortest path from start to end +var result = SuperEnumerable + .GetShortestPaths( + "start", + (state, cost) => map[state] + .Select(x => (x.to, x.cost + cost)), + StringComparer.OrdinalIgnoreCase, + default); + +foreach (var (key, (from, cost)) in result) +{ + Console.WriteLine($"[{key}] = (from: {from}, totalCost: {cost})"); +} + +// This code produces the following output: +// [start] = (from: , totalCost: 0) +// [a] = (from: start, totalCost: 1) +// [b] = (from: a, totalCost: 3) +// [c] = (from: b, totalCost: 6) +// [END] = (from: start, totalCost: 10) +// [d] = (from: c, totalCost: 10) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent1.linq new file mode 100644 index 000000000..2c137a4e5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent1.linq @@ -0,0 +1,35 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 1, value: 123), + (key: 1, value: 456), + (key: 1, value: 789), + (key: 2, value: 987), + (key: 2, value: 654), + (key: 2, value: 321), + (key: 3, value: 789), + (key: 3, value: 456), + (key: 3, value: 123), + (key: 1, value: 123), + (key: 1, value: 456), + (key: 1, value: 781), +}; + +// Group adjacent items +var result = sequence + .GroupAdjacent( + x => x.key); + +Console.WriteLine( + "[ " + + string.Join( + ", ", + result.Select(c => "[" + string.Join(", ", c) + "]")) + + " ]"); + +// This code produces the following output: +// [ [(1, 123), (1, 456), (1, 789)], [(2, 987), (2, 654), (2, 321)], [(3, 789), (3, 456), (3, 123)], [(1, 123), (1, 456), (1, 781)] ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent2.linq new file mode 100644 index 000000000..4b9f40cd5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent2.linq @@ -0,0 +1,36 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 789), + (key: "feb", value: 987), + (key: "Feb", value: 654), + (key: "FEB", value: 321), + (key: "mar", value: 789), + (key: "Mar", value: 456), + (key: "MAR", value: 123), + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 781), +}; + +// Group adjacent items +var result = sequence + .GroupAdjacent( + x => x.key, + StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[ " + + string.Join( + ", ", + result.Select(c => "[" + string.Join(", ", c) + "]")) + + " ]"); + +// This code produces the following output: +// [ [(jan, 123), (Jan, 456), (JAN, 789)], [(feb, 987), (Feb, 654), (FEB, 321)], [(mar, 789), (Mar, 456), (MAR, 123)], [(jan, 123), (Jan, 456), (JAN, 781)] ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent3.linq new file mode 100644 index 000000000..18e9749db --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent3.linq @@ -0,0 +1,36 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 1, value: 123), + (key: 1, value: 456), + (key: 1, value: 789), + (key: 2, value: 987), + (key: 2, value: 654), + (key: 2, value: 321), + (key: 3, value: 789), + (key: 3, value: 456), + (key: 3, value: 123), + (key: 1, value: 123), + (key: 1, value: 456), + (key: 1, value: 781), +}; + +// Group adjacent items +var result = sequence + .GroupAdjacent( + x => x.key, + x => x.value); + +Console.WriteLine( + "[ " + + string.Join( + ", ", + result.Select(c => "[" + string.Join(", ", c) + "]")) + + " ]"); + +// This code produces the following output: +// [ [(1, 123), (1, 456), (1, 789)], [(2, 987), (2, 654), (2, 321)], [(3, 789), (3, 456), (3, 123)], [(1, 123), (1, 456), (1, 781)] ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent4.linq new file mode 100644 index 000000000..0e172e58e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent4.linq @@ -0,0 +1,37 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 789), + (key: "feb", value: 987), + (key: "Feb", value: 654), + (key: "FEB", value: 321), + (key: "mar", value: 789), + (key: "Mar", value: 456), + (key: "MAR", value: 123), + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 781), +}; + +// Group adjacent items +var result = sequence + .GroupAdjacent( + x => x.key, + x => x.value, + StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[ " + + string.Join( + ", ", + result.Select(c => "[" + string.Join(", ", c) + "]")) + + " ]"); + +// This code produces the following output: +// [ [123, 456, 789], [987, 654, 321], [789, 456, 123], [123, 456, 781] ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent5.linq new file mode 100644 index 000000000..e0eaed2dd --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent5.linq @@ -0,0 +1,36 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 1, value: 123), + (key: 1, value: 456), + (key: 1, value: 789), + (key: 2, value: 987), + (key: 2, value: 654), + (key: 2, value: 321), + (key: 3, value: 789), + (key: 3, value: 456), + (key: 3, value: 123), + (key: 1, value: 123), + (key: 1, value: 456), + (key: 1, value: 781), +}; + +// Group adjacent items +var result = sequence + .GroupAdjacent( + x => x.key, + (k, g) => new { Key = k, Items = "[" + string.Join(", ", g.Select(x => x.value)) + "]", }); + +Console.WriteLine( + "[ " + + string.Join( + ", ", + result) + + " ]"); + +// This code produces the following output: +// [ { Key = 1, Items = [123, 456, 789] }, { Key = 2, Items = [987, 654, 321] }, { Key = 3, Items = [789, 456, 123] }, { Key = 1, Items = [123, 456, 781] } ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent6.linq new file mode 100644 index 000000000..1ac065ebb --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/GroupAdjacent/GroupAdjacent6.linq @@ -0,0 +1,37 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 789), + (key: "feb", value: 987), + (key: "Feb", value: 654), + (key: "FEB", value: 321), + (key: "mar", value: 789), + (key: "Mar", value: 456), + (key: "MAR", value: 123), + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 781), +}; + +// Group adjacent items +var result = sequence + .GroupAdjacent( + x => x.key, + (k, g) => new { Key = k, Items = "[" + string.Join(", ", g.Select(x => x.value)) + "]", }, + StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[ " + + string.Join( + ", ", + result) + + " ]"); + +// This code produces the following output: +// [ { Key = jan, Items = [123, 456, 789] }, { Key = feb, Items = [987, 654, 321] }, { Key = mar, Items = [789, 456, 123] }, { Key = jan, Items = [123, 456, 781] } ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates1.linq new file mode 100644 index 000000000..6873f5ff0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates1.linq @@ -0,0 +1,26 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "foo", "bar", "baz" }; +var seq2 = new[] { "foo", "bar", "baz", "foo", }; +var seq3 = new[] { "foo", "bar", "baz", "FOO", }; + +// determine if a sequence has duplicate items +var result = seq1 + .HasDuplicates(); +Console.WriteLine($"Has Duplicates: {result}"); + +result = seq2 + .HasDuplicates(); +Console.WriteLine($"Has Duplicates: {result}"); + +result = seq3 + .HasDuplicates(); +Console.WriteLine($"Has Duplicates: {result}"); + +// This code produces the following output: +// Has Duplicates: False +// Has Duplicates: True +// Has Duplicates: False diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates2.linq new file mode 100644 index 000000000..dec73a129 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates2.linq @@ -0,0 +1,29 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "foo", "bar", "baz" }; +var seq2 = new[] { "foo", "bar", "baz", "foo", }; +var seq3 = new[] { "foo", "bar", "baz", "FOO", }; + +// determine if a sequence has duplicate items +var result = seq1 + .HasDuplicates( + StringComparer.OrdinalIgnoreCase); +Console.WriteLine($"Has Duplicates: {result}"); + +result = seq2 + .HasDuplicates( + StringComparer.OrdinalIgnoreCase); +Console.WriteLine($"Has Duplicates: {result}"); + +result = seq3 + .HasDuplicates( + StringComparer.OrdinalIgnoreCase); +Console.WriteLine($"Has Duplicates: {result}"); + +// This code produces the following output: +// Has Duplicates: False +// Has Duplicates: True +// Has Duplicates: True diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates3.linq new file mode 100644 index 000000000..1a0836e06 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates3.linq @@ -0,0 +1,29 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "f", "ba", "qax" }; +var seq2 = new[] { "f", "ba", "qax", "fuba", }; +var seq3 = new[] { "f", "ba", "qax", "FUBA", }; + +// determine if a sequence has duplicate items +var result = seq1 + .HasDuplicates( + x => x[0..1]); +Console.WriteLine($"Has Duplicates: {result}"); + +result = seq2 + .HasDuplicates( + x => x[0..1]); +Console.WriteLine($"Has Duplicates: {result}"); + +result = seq3 + .HasDuplicates( + x => x[0..1]); +Console.WriteLine($"Has Duplicates: {result}"); + +// This code produces the following output: +// Has Duplicates: False +// Has Duplicates: True +// Has Duplicates: False diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates4.linq new file mode 100644 index 000000000..5f1e0845f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/HasDuplicates/HasDuplicates4.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "f", "ba", "qax" }; +var seq2 = new[] { "f", "ba", "qax", "fuba", }; +var seq3 = new[] { "f", "ba", "qax", "FUBA", }; + +// determine if a sequence has duplicate items +var result = seq1 + .HasDuplicates( + x => x[0..1], + StringComparer.OrdinalIgnoreCase); +Console.WriteLine($"Has Duplicates: {result}"); + +result = seq2 + .HasDuplicates( + x => x[0..1], + StringComparer.OrdinalIgnoreCase); +Console.WriteLine($"Has Duplicates: {result}"); + +result = seq3 + .HasDuplicates( + x => x[0..1], + StringComparer.OrdinalIgnoreCase); +Console.WriteLine($"Has Duplicates: {result}"); + +// This code produces the following output: +// Has Duplicates: False +// Has Duplicates: True +// Has Duplicates: True diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/If/If1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/If/If1.linq new file mode 100644 index 000000000..7b96bcbf0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/If/If1.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Use a function to select which sequence to return values from. +var selector = true; +var result = SuperEnumerable + .If( + () => selector, + sequence); + +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); +selector = false; +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); +selector = true; +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); + +// This code produces the following output: +// Selector: True; result.Count(): 5. +// Selector: False; result.Count(): 0. +// Selector: True; result.Count(): 5. diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/If/If2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/If/If2.linq new file mode 100644 index 000000000..c8d87c39e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/If/If2.linq @@ -0,0 +1,26 @@ + + SuperLinq + SuperLinq + + +var sequence1 = Enumerable.Range(1, 5); +var sequence2 = Enumerable.Range(1, 10); + +// Use a function to select which sequence to return values from. +var selector = true; +var result = SuperEnumerable + .If( + () => selector, + sequence1, + sequence2); + +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); +selector = false; +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); +selector = true; +Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}."); + +// This code produces the following output: +// Selector: True; result.Count(): 5. +// Selector: False; result.Count(): 10. +// Selector: True; result.Count(): 5. diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Index/Index1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Index/Index1.linq new file mode 100644 index 000000000..7d7bdd910 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Index/Index1.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 789), +}; + +// Index a sequence +var result = sequence + .Index(); + +Console.WriteLine( + "[ " + + string.Join(", ", result) + + " ]"); + +// This code produces the following output: +// [ (0, (jan, 123)), (1, (Jan, 456)), (2, (JAN, 789)) ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Index/Index2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Index/Index2.linq new file mode 100644 index 000000000..76e99f558 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Index/Index2.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 789), +}; + +// Index a sequence +var result = sequence + .Index(5); + +Console.WriteLine( + "[ " + + string.Join(", ", result) + + " ]"); + +// This code produces the following output: +// [ (5, (jan, 123)), (6, (Jan, 456)), (7, (JAN, 789)) ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexBy/IndexBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexBy/IndexBy1.linq new file mode 100644 index 000000000..f0caa6929 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexBy/IndexBy1.linq @@ -0,0 +1,28 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + new { Key = 1, Value = 123, }, + new { Key = 2, Value = 987, }, + new { Key = 3, Value = 789, }, + new { Key = 1, Value = 123, }, + new { Key = 1, Value = 781, }, +}; + +// Group adjacent items +var result = sequence + .IndexBy( + x => x.Key); + +Console.WriteLine( + "[ " + + string.Join( + ", ", + result) + + " ]"); + +// This code produces the following output: +// [ (0, { Key = 1, Value = 123 }), (0, { Key = 2, Value = 987 }), (0, { Key = 3, Value = 789 }), (1, { Key = 1, Value = 123 }), (2, { Key = 1, Value = 781 }) ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexBy/IndexBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexBy/IndexBy2.linq new file mode 100644 index 000000000..c91b83910 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexBy/IndexBy2.linq @@ -0,0 +1,29 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + new { Key = "jan", Value = 123, }, + new { Key = "feb", Value = 987, }, + new { Key = "mar", Value = 789, }, + new { Key = "Jan", Value = 123, }, + new { Key = "JAN", Value = 781, }, +}; + +// Group adjacent items +var result = sequence + .IndexBy( + x => x.Key, + StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[ " + + string.Join( + ", ", + result) + + " ]"); + +// This code produces the following output: +// [ (0, { Key = jan, Value = 123 }), (0, { Key = feb, Value = 987 }), (0, { Key = mar, Value = 789 }), (1, { Key = Jan, Value = 123 }), (2, { Key = JAN, Value = 781 }) ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf1.linq new file mode 100644 index 000000000..731ddb725 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf1.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + 1, 2, 3, 4, 5, + 1, 2, 3, 4, 5, +}.AsEnumerable(); + +// Find the element `3` in the sequence +var result = sequence + .IndexOf(3); + +Console.WriteLine($"Index: {result}"); + +// This code produces the following output: +// Index: 2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf2.linq new file mode 100644 index 000000000..05f169972 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf2.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + 1, 2, 3, 4, 5, + 1, 2, 3, 4, 5, +}.AsEnumerable(); + +// Find the element `3` in the sequence in the range [5..] +var result = sequence + .IndexOf(3, 5); + +Console.WriteLine($"Index: {result}"); + +// This code produces the following output: +// Index: 7 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf3.linq new file mode 100644 index 000000000..dfcd29b05 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/IndexOf/IndexOf3.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + 1, 2, 3, 4, 5, + 1, 2, 3, 4, 5, +}.AsEnumerable(); + +// Find the element `3` in the sequence in the range [5..7] +var result = sequence + .IndexOf(3, 5, 2); + +Console.WriteLine($"Index: {result}"); + +// This code produces the following output: +// Index: -1 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Insert/Insert1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Insert/Insert1.linq new file mode 100644 index 000000000..a21dad674 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Insert/Insert1.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var first = Enumerable.Range(1, 5); +var second = Enumerable.Range(1, 5); + +// Insert one sequence into another +var result = first + .Insert(second, 2); + +Console.WriteLine(string.Join(", ", result)); + +// This code produces the following output: +// 1, 2, 1, 2, 3, 4, 5, 3, 4, 5 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Insert/Insert2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Insert/Insert2.linq new file mode 100644 index 000000000..e5d72f7f5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Insert/Insert2.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +var first = Enumerable.Range(1, 5); +var second = Enumerable.Range(1, 5); + +// Insert one sequence into another +var result = first + .Insert(second, ^2); + +Console.WriteLine(string.Join(", ", result)); + +// This code produces the following output: +// 1, 2, 3, 1, 2, 3, 4, 5, 4, 5 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Interleave/Interleave1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Interleave/Interleave1.linq new file mode 100644 index 000000000..c6a65eeaa --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Interleave/Interleave1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var seq1 = Enumerable.Range(1, 5); +var seq2 = Enumerable.Range(1, 4); +var seq3 = Enumerable.Range(1, 3); + +// Interleave the elements from multiple sequences into a single sequence +var result = seq1 + .Interleave(seq2, seq3); + +Console.WriteLine(string.Join(", ", result)); + +// This code produces the following output: +// 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Interleave/Interleave2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Interleave/Interleave2.linq new file mode 100644 index 000000000..880402259 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Interleave/Interleave2.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var seq1 = Enumerable.Range(1, 5); +var seq2 = Enumerable.Range(1, 4); +var seq3 = Enumerable.Range(1, 3); + +// Interleave the elements from multiple sequences into a single sequence +var result = new[] { seq1, seq2, seq3, } + .Interleave(); + +Console.WriteLine(string.Join(", ", result)); + +// This code produces the following output: +// 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag1.linq new file mode 100644 index 000000000..f6939ab32 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "baz" }; + +// get lagged elements +var result = sequence.Lag(1); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(foo, ), (bar, foo), (baz, bar)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag2.linq new file mode 100644 index 000000000..e742d9b04 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag2.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "baz" }; + +// get lagged elements +var result = sequence.Lag(1, (cur, lag) => new { cur, lag, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ cur = foo, lag = }, { cur = bar, lag = foo }, { cur = baz, lag = bar }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag3.linq new file mode 100644 index 000000000..9ab25719d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lag/Lag3.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "baz" }; + +// get lagged elements +var result = sequence.Lag(1, "LAG", (cur, lag) => new { cur, lag, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ cur = foo, lag = LAG }, { cur = bar, lag = foo }, { cur = baz, lag = bar }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf1.linq new file mode 100644 index 000000000..e33a4eb40 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf1.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + 1, 2, 3, 4, 5, + 1, 2, 3, 4, 5, +}.AsEnumerable(); + +// Find the element `3` in the sequence +var result = sequence + .LastIndexOf(3); + +Console.WriteLine($"Index: {result}"); + +// This code produces the following output: +// Index: 7 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf2.linq new file mode 100644 index 000000000..483e4dfaa --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf2.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + 1, 2, 3, 4, 5, + 1, 2, 3, 4, 5, +}.AsEnumerable(); + +// Find the element `3` in the sequence in the range [5..] +var result = sequence + .LastIndexOf(3, 5); + +Console.WriteLine($"Index: {result}"); + +// This code produces the following output: +// Index: 7 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf3.linq new file mode 100644 index 000000000..a959f1e78 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/LastIndexOf/LastIndexOf3.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + 1, 2, 3, 4, 5, + 1, 2, 3, 4, 5, +}.AsEnumerable(); + +// Find the element `3` in the sequence in the range [0..3] +var result = sequence + .IndexOf(3, 0, 3); + +Console.WriteLine($"Index: {result}"); + +// This code produces the following output: +// Index: 2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead1.linq new file mode 100644 index 000000000..cb3db7547 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "baz" }; + +// get leading elements +var result = sequence.Lead(1); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(foo, bar), (bar, baz), (baz, )] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead2.linq new file mode 100644 index 000000000..137d33c5d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead2.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "baz" }; + +// get leading elements +var result = sequence.Lead(1, (cur, lead) => new { cur, lead, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ cur = foo, lead = bar }, { cur = bar, lead = baz }, { cur = baz, lead = }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead3.linq new file mode 100644 index 000000000..c12de18b4 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Lead/Lead3.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "baz" }; + +// get leading elements +var result = sequence.Lead(1, "LEAD", (cur, lag) => new { cur, lag, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ cur = foo, lag = bar }, { cur = bar, lag = baz }, { cur = baz, lag = LEAD }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems1.linq new file mode 100644 index 000000000..ec540a034 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems1.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + "A", "B", "C", "D", "F", + "a", "b", "c", "d", "f", +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MaxItems(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [F] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems2.linq new file mode 100644 index 000000000..81ba80260 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems2.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + "A", "B", "C", "D", "F", + "a", "b", "c", "d", "f", +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MaxItems(StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [F] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems3.linq new file mode 100644 index 000000000..da64b023c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems3.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (cnt: 1, item: "A"), + (cnt: 2, item: "B"), + (cnt: 3, item: "C"), + (cnt: 4, item: "D"), + (cnt: 5, item: "E"), + (cnt: 1, item: "a"), + (cnt: 2, item: "b"), + (cnt: 3, item: "c"), + (cnt: 4, item: "d"), + (cnt: 5, item: "e"), +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MaxItemsBy(x => x.cnt); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, E), (5, e)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems4.linq new file mode 100644 index 000000000..bc9b2d7d4 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems4.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (cnt: 1, item: "A"), + (cnt: 2, item: "B"), + (cnt: 3, item: "C"), + (cnt: 4, item: "D"), + (cnt: 5, item: "E"), + (cnt: 1, item: "a"), + (cnt: 2, item: "b"), + (cnt: 3, item: "c"), + (cnt: 4, item: "d"), + (cnt: 5, item: "e"), +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MaxItemsBy(x => x.item, StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, E), (5, e)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems5.linq new file mode 100644 index 000000000..0d7305b33 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems5.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (cnt: 1, item: "A"), + (cnt: 2, item: "B"), + (cnt: 3, item: "C"), + (cnt: 4, item: "D"), + (cnt: 5, item: "E"), + (cnt: 1, item: "a"), + (cnt: 2, item: "b"), + (cnt: 3, item: "c"), + (cnt: 4, item: "d"), + (cnt: 5, item: "e"), +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MaxByWithTies(x => x.cnt); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, E), (5, e)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems6.linq new file mode 100644 index 000000000..16246ecde --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MaxItems/MaxItems6.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (cnt: 1, item: "A"), + (cnt: 2, item: "B"), + (cnt: 3, item: "C"), + (cnt: 4, item: "D"), + (cnt: 5, item: "E"), + (cnt: 1, item: "a"), + (cnt: 2, item: "b"), + (cnt: 3, item: "c"), + (cnt: 4, item: "d"), + (cnt: 5, item: "e"), +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MaxByWithTies(x => x.item, StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, E), (5, e)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Memoize/Memoize.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Memoize/Memoize.linq new file mode 100644 index 000000000..8e22fae46 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Memoize/Memoize.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var count = 0; +var sequence = Enumerable.Range(1, 10) + .Do(_ => count++); + +// get leading elements +var result = sequence.Memoize(); + +Console.WriteLine($"iterations: {count}"); +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); +Console.WriteLine($"iterations: {count}"); +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); +Console.WriteLine($"iterations: {count}"); + +// This code produces the following output: +// iterations: 0 +// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +// iterations: 10 +// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +// iterations: 10 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems1.linq new file mode 100644 index 000000000..7187d9f8b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems1.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + "A", "B", "C", "D", "F", + "a", "b", "c", "d", "f", +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MinItems(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [a] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems2.linq new file mode 100644 index 000000000..cc60fae0d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems2.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + "A", "B", "C", "D", "F", + "a", "b", "c", "d", "f", +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MinItems(StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [A, a] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems3.linq new file mode 100644 index 000000000..d6ed8bd6f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems3.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (cnt: 1, item: "A"), + (cnt: 2, item: "B"), + (cnt: 3, item: "C"), + (cnt: 4, item: "D"), + (cnt: 5, item: "E"), + (cnt: 1, item: "a"), + (cnt: 2, item: "b"), + (cnt: 3, item: "c"), + (cnt: 4, item: "d"), + (cnt: 5, item: "e"), +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MinItemsBy(x => x.cnt); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, A), (1, a)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems4.linq new file mode 100644 index 000000000..3eb77a803 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems4.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (cnt: 1, item: "A"), + (cnt: 2, item: "B"), + (cnt: 3, item: "C"), + (cnt: 4, item: "D"), + (cnt: 5, item: "E"), + (cnt: 1, item: "a"), + (cnt: 2, item: "b"), + (cnt: 3, item: "c"), + (cnt: 4, item: "d"), + (cnt: 5, item: "e"), +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MinItemsBy(x => x.item, StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, A), (1, a)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems5.linq new file mode 100644 index 000000000..61ca4538e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems5.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (cnt: 1, item: "A"), + (cnt: 2, item: "B"), + (cnt: 3, item: "C"), + (cnt: 4, item: "D"), + (cnt: 5, item: "E"), + (cnt: 1, item: "a"), + (cnt: 2, item: "b"), + (cnt: 3, item: "c"), + (cnt: 4, item: "d"), + (cnt: 5, item: "e"), +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MinByWithTies(x => x.cnt); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, A), (1, a)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems6.linq new file mode 100644 index 000000000..0f0130df4 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/MinItems/MinItems6.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (cnt: 1, item: "A"), + (cnt: 2, item: "B"), + (cnt: 3, item: "C"), + (cnt: 4, item: "D"), + (cnt: 5, item: "E"), + (cnt: 1, item: "a"), + (cnt: 2, item: "b"), + (cnt: 3, item: "c"), + (cnt: 4, item: "d"), + (cnt: 5, item: "e"), +}.AsEnumerable(); + +// Find the maximum items of the sequence +var result = sequence + .MinByWithTies(x => x.item, StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, A), (1, a)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Move/Move.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Move/Move.linq new file mode 100644 index 000000000..9a16c8d36 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Move/Move.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 6); + +// move a subsequence within the larger sequence +var result = sequence + .Move(3, 2, 0); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [3, 4, 0, 1, 2, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext1.linq new file mode 100644 index 000000000..e1459f574 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext1.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 6th element +var sequence = Enumerable.Range(1, 5) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Skip over the error and enumerate the second sequence +var result = sequence + .OnErrorResumeNext( + Enumerable.Range(1, 5)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext2.linq new file mode 100644 index 000000000..f72e14f4f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext2.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 6th element +var sequence = Enumerable.Range(1, 5) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Skip over the error and enumerate the second sequence +var result = SuperEnumerable + .OnErrorResumeNext( + sequence, + Enumerable.Range(1, 5)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext3.linq new file mode 100644 index 000000000..dd527218f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OnErrorResumeNext/OnErrorResumeNext3.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 6th element +var sequence = Enumerable.Range(1, 5) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Skip over the error and enumerate the second sequence +var result = new[] { sequence, Enumerable.Range(1, 5), } + .OnErrorResumeNext(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/OrderBy/OrderBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OrderBy/OrderBy1.linq new file mode 100644 index 000000000..773194e54 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OrderBy/OrderBy1.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var fruits = new[] +{ + "grape", "passionfruit", "banana", "Mango", + "Orange", "raspberry", "apple", "blueberry", +}; + +// Sort the strings first by their length and then +// alphabetically by passing the identity selector function. +var result = fruits + .OrderBy(fruit => fruit.Length) + .ThenBy(fruit => fruit); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [apple, grape, Mango, banana, Orange, blueberry, raspberry, passionfruit] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/OrderBy/OrderBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OrderBy/OrderBy2.linq new file mode 100644 index 000000000..b3a5cee7a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/OrderBy/OrderBy2.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var fruits = new[] +{ + "grape", "passionfruit", "banana", "Mango", + "Orange", "raspberry", "apple", "blueberry", +}; + +// Sort the strings first by their length and then +// alphabetically by passing the identity selector function. +var result = fruits + .OrderBy(fruit => fruit.Length) + .ThenBy(fruit => fruit, StringComparer.Ordinal); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [Mango, apple, grape, Orange, banana, blueberry, raspberry, passionfruit] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad1.linq new file mode 100644 index 000000000..2105f7913 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Pad a sequence until it is at least a certain length +var result = sequence + .Pad(6); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 0, 0, 0] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad2.linq new file mode 100644 index 000000000..662f3a60f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad2.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Pad a sequence until it is at least a certain length +var result = sequence + .Pad(6, -5); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, -5, -5, -5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad3.linq new file mode 100644 index 000000000..2b6fde1fa --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Pad/Pad3.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Pad a sequence until it is at least a certain length +var result = sequence + .Pad(6, i => -i); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, -3, -4, -5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart1.linq new file mode 100644 index 000000000..9f663750d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Pad a sequence until it is at least a certain length +var result = sequence + .PadStart(6); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0, 0, 0, 1, 2, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart2.linq new file mode 100644 index 000000000..53466c185 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart2.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Pad a sequence until it is at least a certain length +var result = sequence + .PadStart(6, -5); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [-5, -5, -5, 1, 2, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart3.linq new file mode 100644 index 000000000..20426f990 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PadStart/PadStart3.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Pad a sequence until it is at least a certain length +var result = sequence + .PadStart(6, i => -i); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0, -1, -2, 1, 2, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort1.linq new file mode 100644 index 000000000..2c83188b0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort1.linq @@ -0,0 +1,47 @@ + + SuperLinq + SuperLinq + + +var sequence = new Item[] +{ + new(key: 5, text: "1"), + new(key: 5, text: "2"), + new(key: 4, text: "3"), + new(key: 4, text: "4"), + new(key: 3, text: "5"), + new(key: 3, text: "6"), + new(key: 2, text: "7"), + new(key: 2, text: "8"), + new(key: 1, text: "9"), + new(key: 1, text: "10"), +}; + +// Get the top N items +var result = sequence.PartialSort(3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 9), (1, 10), (2, 7)] + +class Item : IComparable +{ + public Item(int key, string text) + { + Key = key; + Text = text; + } + + public int Key { get; } + public string Text { get; } + + public int CompareTo(Item other) => + this.Key.CompareTo(other.Key); + + public override string ToString() => + $"({this.Key}, {this.Text})"; +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort2.linq new file mode 100644 index 000000000..f500f4d75 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort2.linq @@ -0,0 +1,47 @@ + + SuperLinq + SuperLinq + + +var sequence = new Item[] +{ + new(key: 5, text: "1"), + new(key: 5, text: "2"), + new(key: 4, text: "3"), + new(key: 4, text: "4"), + new(key: 3, text: "5"), + new(key: 3, text: "6"), + new(key: 2, text: "7"), + new(key: 2, text: "8"), + new(key: 1, text: "9"), + new(key: 1, text: "10"), +}; + +// Get the top N items +var result = sequence.PartialSort(3, OrderByDirection.Descending); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, 1), (5, 2), (4, 3)] + +class Item : IComparable +{ + public Item(int key, string text) + { + Key = key; + Text = text; + } + + public int Key { get; } + public string Text { get; } + + public int CompareTo(Item other) => + this.Key.CompareTo(other.Key); + + public override string ToString() => + $"({this.Key}, {this.Text})"; +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort3.linq new file mode 100644 index 000000000..0dbb10c7b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort3.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N items +var result = sequence + .PartialSort( + 3, + Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key))); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 9), (1, 10), (2, 7)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort4.linq new file mode 100644 index 000000000..8ecf4dd67 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSort/PartialSort4.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N items +var result = sequence + .PartialSort( + 3, + Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key)), + OrderByDirection.Descending); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, 1), (5, 2), (4, 3)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy1.linq new file mode 100644 index 000000000..3b4092e8e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy1.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N items +var result = sequence + .PartialSortBy( + 3, + x => x.key); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 9), (1, 10), (2, 7)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy2.linq new file mode 100644 index 000000000..c721fbb67 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy2.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N items +var result = sequence + .PartialSortBy( + 3, + x => x.key, + OrderByDirection.Descending); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, 1), (5, 2), (4, 3)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy3.linq new file mode 100644 index 000000000..713c77ce4 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy3.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N items +var result = sequence + .PartialSortBy( + 1, + x => x.key, + Comparer.Create((x, y) => (x % 2).CompareTo(y % 2))); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(4, 3)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy4.linq new file mode 100644 index 000000000..85636a709 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PartialSortBy/PartialSortBy4.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Get the top N items +var result = sequence + .PartialSortBy( + 1, + x => x.key, + Comparer.Create((x, y) => (x % 2).CompareTo(y % 2)), + OrderByDirection.Descending); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(5, 1), (5, 2), (3, 5), (3, 6), (1, 9), (1, 10)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Partition/Partition1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Partition/Partition1.linq new file mode 100644 index 000000000..f3cbc3a11 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Partition/Partition1.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 10); + +// Partition a sequence +var (evens, odds) = sequence + .Partition(x => x % 2 == 0); + +Console.WriteLine( + "evens: [" + + string.Join(", ", evens) + + "]"); + +Console.WriteLine( + "odds: [" + + string.Join(", ", odds) + + "]"); + +// This code produces the following output: +// evens: [0, 2, 4, 6, 8] +// odds: [1, 3, 5, 7, 9] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Partition/Partition2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Partition/Partition2.linq new file mode 100644 index 000000000..852edf5a5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Partition/Partition2.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 10); + +// Partition a sequence +var (evens, odds) = sequence + .Partition(x => x % 2 == 0, ValueTuple.Create); + +Console.WriteLine( + "evens: [" + + string.Join(", ", evens) + + "]"); + +Console.WriteLine( + "odds: [" + + string.Join(", ", odds) + + "]"); + +// This code produces the following output: +// evens: [0, 2, 4, 6, 8] +// odds: [1, 3, 5, 7, 9] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Permutations/Permutations.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Permutations/Permutations.linq new file mode 100644 index 000000000..cf74eba6d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Permutations/Permutations.linq @@ -0,0 +1,44 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 4); + +// Partition a sequence +var result = sequence.Permutations(); + +Console.WriteLine( + $""" + [ + {string.Join(Environment.NewLine, result.Select(r => "\t[" + string.Join(", ", r) + "]"))} + ] + """); + +// This code produces the following output: +// [ +// [0, 1, 2, 3] +// [0, 1, 3, 2] +// [0, 2, 1, 3] +// [0, 2, 3, 1] +// [0, 3, 1, 2] +// [0, 3, 2, 1] +// [1, 0, 2, 3] +// [1, 0, 3, 2] +// [1, 2, 0, 3] +// [1, 2, 3, 0] +// [1, 3, 0, 2] +// [1, 3, 2, 0] +// [2, 0, 1, 3] +// [2, 0, 3, 1] +// [2, 1, 0, 3] +// [2, 1, 3, 0] +// [2, 3, 0, 1] +// [2, 3, 1, 0] +// [3, 0, 1, 2] +// [3, 0, 2, 1] +// [3, 1, 0, 2] +// [3, 1, 2, 0] +// [3, 2, 0, 1] +// [3, 2, 1, 0] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/PreScan/PreScan.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PreScan/PreScan.linq new file mode 100644 index 000000000..a999f9d8c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/PreScan/PreScan.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 4); + +// execute a scan of the sequence, returning the aggregation before processing the element +var result = sequence.PreScan((a, b) => a + b, 0); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0, 1, 3, 6] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Publish/Publish1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Publish/Publish1.linq new file mode 100644 index 000000000..3085e9caf --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Publish/Publish1.linq @@ -0,0 +1,42 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 10); + +// allow multiple consumers to cache views of the same sequence +using var rng = sequence.Publish(); +using var e1 = rng.GetEnumerator(); // e1 has a view on the source starting from element 0 + +Debug.Assert(e1.MoveNext()); +Console.WriteLine("e1.MoveNext()"); +Console.WriteLine($"e1.Current: {e1.Current}"); + +Debug.Assert(e1.MoveNext()); +Console.WriteLine("e1.MoveNext()"); +Console.WriteLine($"e1.Current: {e1.Current}"); + +using var e2 = rng.GetEnumerator(); + +Debug.Assert(e2.MoveNext()); // e2 has a view on the source starting from element 2 +Console.WriteLine("e2.MoveNext()"); +Console.WriteLine($"e1.Current: {e1.Current}"); +Console.WriteLine($"e2.Current: {e2.Current}"); + +Debug.Assert(e1.MoveNext()); // e1 continues to enumerate over its view +Console.WriteLine("e1.MoveNext()"); +Console.WriteLine($"e1.Current: {e1.Current}"); +Console.WriteLine($"e2.Current: {e2.Current}"); + +// This code produces the following output: +// e1.MoveNext() +// e1.Current: 0 +// e1.MoveNext() +// e1.Current: 1 +// e2.MoveNext() +// e1.Current: 1 +// e2.Current: 2 +// e1.MoveNext() +// e1.Current: 2 +// e2.Current: 2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/RandomSubset/RandomSubset1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RandomSubset/RandomSubset1.linq new file mode 100644 index 000000000..06a04d055 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RandomSubset/RandomSubset1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// get a random subset of the above sequence +var result = sequence.RandomSubset(4); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible output of the above sequence: +// (each run will have different results) +// [3, 6, 7, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/RandomSubset/RandomSubset2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RandomSubset/RandomSubset2.linq new file mode 100644 index 000000000..0c60e0ba0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RandomSubset/RandomSubset2.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// get a random subset of the above sequence +var result = sequence.RandomSubset(4, new Random(Seed: 5)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [7, 8, 2, 6] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Range/Range.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Range/Range.linq new file mode 100644 index 000000000..11a19bc5d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Range/Range.linq @@ -0,0 +1,16 @@ + + SuperLinq + SuperLinq + + +// Generate a sequence using a sequence function +var result = SuperEnumerable + .Range(1, 10, 2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Rank/Rank1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Rank/Rank1.linq new file mode 100644 index 000000000..48eb9ce3b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Rank/Rank1.linq @@ -0,0 +1,47 @@ + + SuperLinq + SuperLinq + + +var sequence = new Item[] +{ + new(key: 5, text: "1"), + new(key: 5, text: "2"), + new(key: 4, text: "3"), + new(key: 4, text: "4"), + new(key: 3, text: "5"), + new(key: 3, text: "6"), + new(key: 2, text: "7"), + new(key: 2, text: "8"), + new(key: 1, text: "9"), + new(key: 1, text: "10"), +}; + +// Rank the items in the sequence +var result = sequence.Rank(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [((1, 9), 1), ((1, 10), 1), ((2, 7), 3), ((2, 8), 3), ((3, 5), 5), ((3, 6), 5), ((4, 3), 7), ((4, 4), 7), ((5, 1), 9), ((5, 2), 9)] + +class Item : IComparable +{ + public Item(int key, string text) + { + Key = key; + Text = text; + } + + public int Key { get; } + public string Text { get; } + + public int CompareTo(Item other) => + this.Key.CompareTo(other.Key); + + public override string ToString() => + $"({this.Key}, {this.Text})"; +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Rank/Rank3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Rank/Rank3.linq new file mode 100644 index 000000000..16e3b984d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Rank/Rank3.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Rank the items in the sequence +var result = sequence + .Rank( + Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key))); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [((1, 9), 1), ((1, 10), 1), ((2, 7), 3), ((2, 8), 3), ((3, 5), 5), ((3, 6), 5), ((4, 3), 7), ((4, 4), 7), ((5, 1), 9), ((5, 2), 9)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/RankBy/RankBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RankBy/RankBy1.linq new file mode 100644 index 000000000..62f86d40d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RankBy/RankBy1.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Rank the items in the sequence +var result = sequence + .RankBy( + x => x.key); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [((1, 9), 1), ((1, 10), 1), ((2, 7), 3), ((2, 8), 3), ((3, 5), 5), ((3, 6), 5), ((4, 3), 7), ((4, 4), 7), ((5, 1), 9), ((5, 2), 9)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/RankBy/RankBy3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RankBy/RankBy3.linq new file mode 100644 index 000000000..d43cc801b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RankBy/RankBy3.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: 5, text: "1"), + (key: 5, text: "2"), + (key: 4, text: "3"), + (key: 4, text: "4"), + (key: 3, text: "5"), + (key: 3, text: "6"), + (key: 2, text: "7"), + (key: 2, text: "8"), + (key: 1, text: "9"), + (key: 1, text: "10"), +}; + +// Rank the items in the sequence +var result = sequence + .RankBy( + x => x.key, + Comparer.Create((x, y) => (x % 2).CompareTo(y % 2))); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [((4, 3), 1), ((4, 4), 1), ((2, 7), 1), ((2, 8), 1), ((5, 1), 5), ((5, 2), 5), ((3, 5), 5), ((3, 6), 5), ((1, 9), 5), ((1, 10), 5)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat1.linq new file mode 100644 index 000000000..83005b1e0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +// Repeat a value indefinitely +var result = SuperEnumerable + .Repeat(1) + .Take(10); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat2.linq new file mode 100644 index 000000000..c53af30df --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat2.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Repeat a sequence indefinitely +var result = sequence + .Repeat() + .Take(10); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 1, 2, 3, 1, 2, 3, 1] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat3.linq new file mode 100644 index 000000000..c143bb27f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Repeat/Repeat3.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 3); + +// Repeat a sequence +var result = sequence + .Repeat(3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 1, 2, 3, 1, 2, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Replace/Replace1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Replace/Replace1.linq new file mode 100644 index 000000000..bddace9dc --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Replace/Replace1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Replace a value in a sequence +var result = sequence + .Replace(3, -100); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, -100, 5, 6, 7, 8, 9, 10] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Replace/Replace2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Replace/Replace2.linq new file mode 100644 index 000000000..794753eb7 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Replace/Replace2.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Replace a value in a sequence +var result = sequence + .Replace(^3, -100); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5, 6, 7, -100, 9, 10] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Retry/Retry1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Retry/Retry1.linq new file mode 100644 index 000000000..223fbf357 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Retry/Retry1.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 4th element +var sequence = Enumerable.Range(1, 3) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Re-enumerate the sequence on failure indefinitely +var result = sequence + .Retry() + .Take(12); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Retry/Retry2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Retry/Retry2.linq new file mode 100644 index 000000000..83df5908b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Retry/Retry2.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +// this sequence will throw an exception on the 4th element +var sequence = Enumerable.Range(1, 3) + .Concat(SuperEnumerable.Throw(new InvalidOperationException())); + +// Re-enumerate the sequence on failure up to n times +var result = sequence + .Retry(5) + .Take(12); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Return/Return.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Return/Return.linq new file mode 100644 index 000000000..9a2a8e08c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Return/Return.linq @@ -0,0 +1,15 @@ + + SuperLinq + SuperLinq + + +// Return a sequence of a single element +var result = SuperEnumerable.Return(123); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [123] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/RunLengthEncode/RunLengthEncode1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RunLengthEncode/RunLengthEncode1.linq new file mode 100644 index 000000000..9205be7b8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RunLengthEncode/RunLengthEncode1.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Repeat(1, 3) + .Concat(Enumerable.Repeat(2, 4)) + .Concat(Enumerable.Repeat(3, 2)); + +// Get the run-length encoding of a sequence +var result = sequence.RunLengthEncode(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 3), (2, 4), (3, 2)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/RunLengthEncode/RunLengthEncode2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RunLengthEncode/RunLengthEncode2.linq new file mode 100644 index 000000000..105c9542d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/RunLengthEncode/RunLengthEncode2.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + "foo", + "FOO", + "fOo", + "bAr", + "BAR", + "BAZ", + "baz", + "Baz", + "BaZ", + "Qux", +}; + +// Get the run-length encoding of a sequence +var result = sequence + .RunLengthEncode( + StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(foo, 3), (bAr, 2), (BAZ, 4), (Qux, 1)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Scan/Scan1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Scan/Scan1.linq new file mode 100644 index 000000000..38938c26a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Scan/Scan1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 4); + +// execute a scan of the sequence +var result = sequence.Scan((a, b) => a + b); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 3, 6, 10] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Scan/Scan2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Scan/Scan2.linq new file mode 100644 index 000000000..c6d21d896 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Scan/Scan2.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 4); + +// execute a scan of the sequence +var result = sequence + .Scan( + "0", + (a, b) => $"({a} + {b})"); + +Console.WriteLine( + "[ \"" + + string.Join("\", \"", result) + + "\" ]"); + +// This code produces the following output: +// [ "0", "(0 + 1)", "((0 + 1) + 2)", "(((0 + 1) + 2) + 3)", "((((0 + 1) + 2) + 3) + 4)" ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanBy/ScanBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanBy/ScanBy1.linq new file mode 100644 index 000000000..aca070f6e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanBy/ScanBy1.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// execute a scan of the sequence +var result = sequence + .ScanBy( + x => x % 2, + k => k * 1000, + (a, k, b) => a + b); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, 1001), (0, 2), (1, 1004), (0, 6), (1, 1009), (0, 12), (1, 1016), (0, 20), (1, 1025), (0, 30)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanBy/ScanBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanBy/ScanBy2.linq new file mode 100644 index 000000000..cb5da612e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanBy/ScanBy2.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + "BAR", + "foo", + "Baz", + "Qux", + "BAZ", + "FOO", + "bAr", + "baz", + "fOo", + "BaZ", +}; + +// execute a scan of the sequence +var result = sequence + .ScanBy( + x => x[..1], + k => 0, + (a, k, b) => a + 1, + StringComparer.OrdinalIgnoreCase); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(B, 1), (f, 1), (B, 2), (Q, 1), (B, 3), (F, 2), (b, 4), (b, 5), (f, 3), (B, 6)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanRight/ScanRight1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanRight/ScanRight1.linq new file mode 100644 index 000000000..f3bef9dc3 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanRight/ScanRight1.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5) + .Select(x => x.ToString()); + +// execute a scan of the sequence +var result = sequence + .ScanRight((a, b) => $"({a}+{b})"); + +Console.WriteLine( + "[ \"" + + string.Join("\", \"", result) + + "\" ]"); + +// This code produces the following output: +// [ "(1+(2+(3+(4+5))))", "(2+(3+(4+5)))", "(3+(4+5))", "(4+5)", "5" ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanRight/ScanRight2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanRight/ScanRight2.linq new file mode 100644 index 000000000..a9e2ffa38 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanRight/ScanRight2.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// execute a scan of the sequence +var result = sequence + .ScanRight( + "6", + (a, b) => $"({a}+{b})"); + +Console.WriteLine( + "[ \"" + + string.Join("\", \"", result) + + "\" ]"); + +// This code produces the following output: +// [ "(1+(2+(3+(4+(5+6)))))", "(2+(3+(4+(5+6))))", "(3+(4+(5+6)))", "(4+(5+6))", "(5+6)", "6" ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment1.linq new file mode 100644 index 000000000..427c93036 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment1.linq @@ -0,0 +1,38 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 789), + (key: "feb", value: 987), + (key: "Feb", value: 654), + (key: "FEB", value: 321), + (key: "mar", value: 789), + (key: "Mar", value: 456), + (key: "MAR", value: 123), + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 781), +}; + +// Group adjacent items +var result = sequence + .Segment( + x => x.key[0] == 'm'); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [(jan, 123), (Jan, 456), (JAN, 789), (feb, 987), (Feb, 654), (FEB, 321)], +// [(mar, 789), (Mar, 456), (MAR, 123), (jan, 123), (Jan, 456), (JAN, 781)] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment2.linq new file mode 100644 index 000000000..5d7501252 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment2.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 789), + (key: "feb", value: 987), + (key: "Feb", value: 654), + (key: "FEB", value: 321), + (key: "mar", value: 789), + (key: "Mar", value: 456), + (key: "MAR", value: 123), + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 781), +}; + +// Group adjacent items +var result = sequence + .Segment( + (x, i) => i % 3 == 0); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [(jan, 123), (Jan, 456), (JAN, 789)], +// [(feb, 987), (Feb, 654), (FEB, 321)], +// [(mar, 789), (Mar, 456), (MAR, 123)], +// [(jan, 123), (Jan, 456), (JAN, 781)] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment3.linq new file mode 100644 index 000000000..a0d204878 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Segment/Segment3.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 789), + (key: "feb", value: 987), + (key: "Feb", value: 654), + (key: "FEB", value: 321), + (key: "mar", value: 789), + (key: "Mar", value: 456), + (key: "MAR", value: 123), + (key: "jan", value: 123), + (key: "Jan", value: 456), + (key: "JAN", value: 781), +}; + +// Group adjacent items +var result = sequence + .Segment( + (cur, prev, i) => !string.Equals(cur.key[..1], prev.key[..1], StringComparison.OrdinalIgnoreCase)); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [(jan, 123), (Jan, 456), (JAN, 789)], +// [(feb, 987), (Feb, 654), (FEB, 321)], +// [(mar, 789), (Mar, 456), (MAR, 123)], +// [(jan, 123), (Jan, 456), (JAN, 781)] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Sequence/Sequence1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Sequence/Sequence1.linq new file mode 100644 index 000000000..cc0df2c25 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Sequence/Sequence1.linq @@ -0,0 +1,25 @@ + + SuperLinq + SuperLinq + + +// Generate a sequence using a sequence function +var result = SuperEnumerable + .Sequence(5, 10); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +result = SuperEnumerable + .Sequence(10, 5); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [5, 6, 7, 8, 9, 10] +// [10, 9, 8, 7, 6, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Sequence/Sequence2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Sequence/Sequence2.linq new file mode 100644 index 000000000..02c5b0c3d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Sequence/Sequence2.linq @@ -0,0 +1,25 @@ + + SuperLinq + SuperLinq + + +// Generate a sequence using a sequence function +var result = SuperEnumerable + .Sequence(5, 10, 2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +result = SuperEnumerable + .Sequence(10, 5, -2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [5, 7, 9] +// [10, 8, 6] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Share/Share.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Share/Share.linq new file mode 100644 index 000000000..7ce432017 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Share/Share.linq @@ -0,0 +1,42 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 10); + +// allow multiple consumers to cache views of the same sequence +using var rng = sequence.Share(); +using var e1 = rng.GetEnumerator(); // e1 has a shared view on the source + +Debug.Assert(e1.MoveNext()); +Console.WriteLine("e1.MoveNext()"); +Console.WriteLine($"e1.Current: {e1.Current}"); + +Debug.Assert(e1.MoveNext()); +Console.WriteLine("e1.MoveNext()"); +Console.WriteLine($"e1.Current: {e1.Current}"); + +using var e2 = rng.GetEnumerator(); // e2 has a shared view on the source + +Debug.Assert(e2.MoveNext()); // e2 enumerates over the shared view, advancing the source +Console.WriteLine("e2.MoveNext()"); +Console.WriteLine($"e1.Current: {e1.Current}"); +Console.WriteLine($"e2.Current: {e2.Current}"); + +Debug.Assert(e1.MoveNext()); // e1 enumerates over the shared view, advancing the source +Console.WriteLine("e1.MoveNext()"); +Console.WriteLine($"e1.Current: {e1.Current}"); +Console.WriteLine($"e2.Current: {e2.Current}"); + +// This code produces the following output: +// e1.MoveNext() +// e1.Current: 0 +// e1.MoveNext() +// e1.Current: 1 +// e2.MoveNext() +// e1.Current: 1 +// e2.Current: 2 +// e1.MoveNext() +// e1.Current: 3 +// e2.Current: 2 diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Shuffle/Shuffle1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Shuffle/Shuffle1.linq new file mode 100644 index 000000000..667444375 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Shuffle/Shuffle1.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Shuffle a sequence +var result = sequence.Shuffle(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [10, 9, 3, 8, 1, 6, 2, 4, 7, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Shuffle/Shuffle2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Shuffle/Shuffle2.linq new file mode 100644 index 000000000..731ce3777 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Shuffle/Shuffle2.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Shuffle a sequence +var result = sequence.Shuffle(new Random(10)); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 4, 2, 6, 3, 9, 5, 7, 10, 8] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SkipUntil/SkipUntil.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SkipUntil/SkipUntil.linq new file mode 100644 index 000000000..04b106539 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SkipUntil/SkipUntil.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Skip elements until the condition is true +var result = sequence + .SkipUntil(x => x == 5); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [6, 7, 8, 9, 10] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Slice/Slice.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Slice/Slice.linq new file mode 100644 index 000000000..6b217236b --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Slice/Slice.linq @@ -0,0 +1,17 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Take a slice of the sequence +var result = sequence.Slice(2, 3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge1.linq new file mode 100644 index 000000000..b7f8ab89a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge1.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var s1 = new[] { 3, 7, 11 }; +var s2 = new[] { 2, 4, 20 }; +var s3 = new[] { 17, 19, 25 }; + +// Execute a sorted merge of multiple sequences into a single sequence +var result = s1 + .SortedMerge(s2, s3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [2, 3, 4, 7, 11, 17, 19, 20, 25] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge2.linq new file mode 100644 index 000000000..0b7a32518 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge2.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var s1 = new[] { 3, 7, 11 }; +var s2 = new[] { 2, 4, 20 }; +var s3 = new[] { 17, 19, 25 }; + +// Execute a sorted merge of multiple sequences into a single sequence +var result = s1 + .SortedMerge(Comparer.Default, s2, s3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [2, 3, 4, 7, 11, 17, 19, 20, 25] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge3.linq new file mode 100644 index 000000000..5963286d3 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge3.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var s1 = new[] { 11, 7, 3 }; +var s2 = new[] { 20, 4, 2 }; +var s3 = new[] { 25, 19, 17 }; + +// Execute a sorted merge of multiple sequences into a single sequence +var result = s1 + .SortedMerge(OrderByDirection.Descending, s2, s3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [25, 20, 19, 17, 11, 7, 4, 3, 2] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge4.linq new file mode 100644 index 000000000..bb7d52fdf --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMerge/SortedMerge4.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var s1 = new[] { 11, 7, 3 }; +var s2 = new[] { 20, 4, 2 }; +var s3 = new[] { 25, 19, 17 }; + +// Execute a sorted merge of multiple sequences into a single sequence +var result = s1 + .SortedMerge( + OrderByDirection.Descending, + Comparer.Default, + s2, s3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [25, 20, 19, 17, 11, 7, 4, 3, 2] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy1.linq new file mode 100644 index 000000000..3521f9eff --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy1.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var s1 = new[] { 11, 7, 3 }; +var s2 = new[] { 20, 4, 2 }; +var s3 = new[] { 25, 19, 17 }; + +// Execute a sorted merge of multiple sequences into a single sequence +var result = s1 + .SortedMergeBy(x => -x, s2, s3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [25, 20, 19, 17, 11, 7, 4, 3, 2] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy2.linq new file mode 100644 index 000000000..fb8913cb8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy2.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var s1 = new[] { 11, 7, 3 }; +var s2 = new[] { 20, 4, 2 }; +var s3 = new[] { 25, 19, 17 }; + +// Execute a sorted merge of multiple sequences into a single sequence +var result = s1 + .SortedMergeBy(x => -x, Comparer.Default, s2, s3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [25, 20, 19, 17, 11, 7, 4, 3, 2] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy3.linq new file mode 100644 index 000000000..54638686d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy3.linq @@ -0,0 +1,20 @@ + + SuperLinq + SuperLinq + + +var s1 = new[] { 3, 7, 11 }; +var s2 = new[] { 2, 4, 20 }; +var s3 = new[] { 17, 19, 25 }; + +// Execute a sorted merge of multiple sequences into a single sequence +var result = s1 + .SortedMergeBy(x => -x, OrderByDirection.Descending, s2, s3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [2, 3, 4, 7, 11, 17, 19, 20, 25] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy4.linq new file mode 100644 index 000000000..9ae6f0988 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/SortedMergeBy/SortedMergeBy4.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var s1 = new[] { 3, 7, 11 }; +var s2 = new[] { 2, 4, 20 }; +var s3 = new[] { 17, 19, 25 }; + +// Execute a sorted merge of multiple sequences into a single sequence +var result = s1 + .SortedMergeBy( + x => -x, + OrderByDirection.Descending, + Comparer.Default, + s2, s3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// One possible random output is as follows: +// [2, 3, 4, 7, 11, 17, 19, 20, 25] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split1.linq new file mode 100644 index 000000000..7e9c13337 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split1.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 11); + +// split a sequence using a key value +var result = sequence + .Split(5); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [0, 1, 2, 3, 4], +// [6, 7, 8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split10.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split10.linq new file mode 100644 index 000000000..0505710d8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split10.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 11); + +// split a sequence using a key value +var result = sequence + .Split(x => x % 3 == 2, 2); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [0, 1], +// [3, 4], +// [6, 7, 8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split11.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split11.linq new file mode 100644 index 000000000..f3bdf2637 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split11.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 11); + +// split a sequence using a key value +var result = sequence + .Split(x => x % 3 == 2, g => g.Sum()); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 7, 13, 19] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split12.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split12.linq new file mode 100644 index 000000000..cb6b07621 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split12.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 11); + +// split a sequence using a key value +var result = sequence + .Split(x => x % 3 == 2, 2, g => g.Sum()); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 7, 40] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split2.linq new file mode 100644 index 000000000..5e14b21f5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split2.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 11); + +// split a sequence using a key value +var result = sequence + .Split(5, EqualityComparer.Default); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [0, 1, 2, 3, 4], +// [6, 7, 8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split3.linq new file mode 100644 index 000000000..d92dc1cd9 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split3.linq @@ -0,0 +1,26 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 3).Repeat(10); + +// split a sequence using a key value +var result = sequence + .Split(2, 4); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [0, 1], +// [0, 1], +// [0, 1], +// [0, 1], +// [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split4.linq new file mode 100644 index 000000000..c6ceaff84 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split4.linq @@ -0,0 +1,26 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 3).Repeat(10); + +// split a sequence using a key value +var result = sequence + .Split(2, EqualityComparer.Default, 4); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [0, 1], +// [0, 1], +// [0, 1], +// [0, 1], +// [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split5.linq new file mode 100644 index 000000000..ba76bd75c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split5.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 11); + +// split a sequence using a key value +var result = sequence + .Split(5, g => g.Sum()); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [10, 40] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split6.linq new file mode 100644 index 000000000..c6c2ee601 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split6.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 11); + +// split a sequence using a key value +var result = sequence + .Split(5, EqualityComparer.Default, g => g.Sum()); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [10, 40] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split7.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split7.linq new file mode 100644 index 000000000..91bd17d14 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split7.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 3).Repeat(10); + +// split a sequence using a key value +var result = sequence + .Split(2, 4, g => g.Sum()); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 1, 1, 1, 18] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split8.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split8.linq new file mode 100644 index 000000000..cfa0c1808 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split8.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 3).Repeat(10); + +// split a sequence using a key value +var result = sequence + .Split(2, EqualityComparer.Default, 4, g => g.Sum()); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 1, 1, 1, 18] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split9.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split9.linq new file mode 100644 index 000000000..c4ba0bc58 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Split/Split9.linq @@ -0,0 +1,25 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 11); + +// split a sequence using a key value +var result = sequence + .Split(x => x % 3 == 2); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [0, 1], +// [3, 4], +// [6, 7], +// [9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/StartsWith/StartsWith1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/StartsWith/StartsWith1.linq new file mode 100644 index 000000000..052df5afe --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/StartsWith/StartsWith1.linq @@ -0,0 +1,39 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + "BAR", + "foo", + "Baz", + "Qux", + "BAZ", + "FOO", + "bAr", + "baz", + "fOo", + "BaZ", +}; + +// check that sequence starts with a known sequence of values +var result = sequence + .StartsWith(new[] { "BAR", "foo", "Baz", }); + +Console.WriteLine($"StartsWith ['BAR', 'foo', 'Baz']: {result}"); + +result = sequence + .StartsWith(new[] { "bar", "foo", "Baz", }); + +Console.WriteLine($"StartsWith ['bar', 'foo', 'Baz']: {result}"); + +result = sequence + .StartsWith(new[] { "foo", "Baz", }); + +Console.WriteLine($"StartsWith ['foo', 'Baz']: {result}"); + +// This code produces the following output: +// StartsWith ['BAR', 'foo', 'Baz']: True +// StartsWith ['bar', 'foo', 'Baz']: False +// StartsWith ['foo', 'Baz']: False diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/StartsWith/StartsWith2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/StartsWith/StartsWith2.linq new file mode 100644 index 000000000..fe5618e9a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/StartsWith/StartsWith2.linq @@ -0,0 +1,39 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] +{ + "BAR", + "foo", + "Baz", + "Qux", + "BAZ", + "FOO", + "bAr", + "baz", + "fOo", + "BaZ", +}; + +// check that sequence starts with a known sequence of values +var result = sequence + .StartsWith(new[] { "BAR", "foo", "Baz", }, StringComparer.OrdinalIgnoreCase); + +Console.WriteLine($"StartsWith ['BAR', 'foo', 'Baz']: {result}"); + +result = sequence + .StartsWith(new[] { "bar", "foo", "Baz", }, StringComparer.OrdinalIgnoreCase); + +Console.WriteLine($"StartsWith ['bar', 'foo', 'Baz']: {result}"); + +result = sequence + .StartsWith(new[] { "foo", "Baz", }, StringComparer.OrdinalIgnoreCase); + +Console.WriteLine($"StartsWith ['foo', 'Baz']: {result}"); + +// This code produces the following output: +// StartsWith ['BAR', 'foo', 'Baz']: True +// StartsWith ['bar', 'foo', 'Baz']: True +// StartsWith ['foo', 'Baz']: False diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Subsets/Subsets1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Subsets/Subsets1.linq new file mode 100644 index 000000000..b855c5ba8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Subsets/Subsets1.linq @@ -0,0 +1,37 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 4); + +// check that sequence starts with a known sequence of values +var result = sequence + .Subsets(); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [], +// [0], +// [1], +// [2], +// [3], +// [0, 1], +// [0, 2], +// [0, 3], +// [1, 2], +// [1, 3], +// [2, 3], +// [0, 1, 2], +// [0, 1, 3], +// [0, 2, 3], +// [1, 2, 3], +// [0, 1, 2, 3] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Subsets/Subsets2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Subsets/Subsets2.linq new file mode 100644 index 000000000..aacbd54ba --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Subsets/Subsets2.linq @@ -0,0 +1,27 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(0, 4); + +// check that sequence starts with a known sequence of values +var result = sequence + .Subsets(2); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [0, 1], +// [0, 2], +// [0, 3], +// [1, 2], +// [1, 3], +// [2, 3] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/TagFirstLast/TagFirstLast1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TagFirstLast/TagFirstLast1.linq new file mode 100644 index 000000000..f03d3da53 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TagFirstLast/TagFirstLast1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 4); + +// Replace a value in a sequence +var result = sequence + .TagFirstLast(); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(1, True, False), (2, False, False), (3, False, False), (4, False, True)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/TagFirstLast/TagFirstLast2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TagFirstLast/TagFirstLast2.linq new file mode 100644 index 000000000..64e8b52c5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TagFirstLast/TagFirstLast2.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 4); + +// Replace a value in a sequence +var result = sequence + .TagFirstLast( + (item, first, last) => new + { + Item = item, + IsEdge = first || last, + }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ Item = 1, IsEdge = True }, { Item = 2, IsEdge = False }, { Item = 3, IsEdge = False }, { Item = 4, IsEdge = True }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/TakeEvery/TakeEvery.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TakeEvery/TakeEvery.linq new file mode 100644 index 000000000..f3bc40ddc --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TakeEvery/TakeEvery.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 6); + +// Take every 2nd element of the sequence +var result = sequence + .TakeEvery(2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 3, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/TakeUntil/TakeUntil.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TakeUntil/TakeUntil.linq new file mode 100644 index 000000000..f5f047014 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TakeUntil/TakeUntil.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Take elements until the condition is true +var result = sequence + .TakeUntil(x => x == 5); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex1.linq new file mode 100644 index 000000000..0bc333704 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "alp", "car", }; + +// Transform a sequence by index +var result = sequence + .ToArrayByIndex(c => c[0] - 'a'); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [alp, bar, car, , , foo] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex2.linq new file mode 100644 index 000000000..ee7afc0c9 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex2.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "alp", "car", }; + +// Transform a sequence by index +var result = sequence + .ToArrayByIndex(26, c => c[0] - 'a'); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [alp, bar, car, , , foo, , , , , , , , , , , , , , , , , , , , ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex3.linq new file mode 100644 index 000000000..c50487384 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex3.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "alp", "car", }; + +// Transform a sequence by index +var result = sequence + .ToArrayByIndex(c => c[0] - 'a', c => $"{c[0] - 'a'}:{c}"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0:alp, 1:bar, 2:car, , , 5:foo] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex4.linq new file mode 100644 index 000000000..1b75e6cf0 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex4.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "alp", "car", }; + +// Transform a sequence by index +var result = sequence + .ToArrayByIndex(c => c[0] - 'a', (c, i) => $"{i}:{c}"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0:alp, 1:bar, 2:car, , , 5:foo] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex5.linq new file mode 100644 index 000000000..72c947631 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex5.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "alp", "car", }; + +// Transform a sequence by index +var result = sequence + .ToArrayByIndex(26, c => c[0] - 'a', c => $"{c[0] - 'a'}:{c}"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0:alp, 1:bar, 2:car, , , 5:foo, , , , , , , , , , , , , , , , , , , , ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex6.linq new file mode 100644 index 000000000..42091d55f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ToArrayByIndex/ToArrayByIndex6.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "foo", "bar", "alp", "car", }; + +// Transform a sequence by index +var result = sequence + .ToArrayByIndex(26, c => c[0] - 'a', (c, i) => $"{i}:{c}"); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0:alp, 1:bar, 2:car, , , 5:foo, , , , , , , , , , , , , , , , , , , , ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Transpose/Transpose.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Transpose/Transpose.linq new file mode 100644 index 000000000..8b546c1b8 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Transpose/Transpose.linq @@ -0,0 +1,28 @@ + + SuperLinq + SuperLinq + + +var matrix = new[] +{ + new[] { 10, 11 }, + new[] { 20 }, + new[] { 30, 31, 32 } +}; + +// Transpose a 2d sequence +var result = matrix.Transpose(); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [10, 20, 30], +// [11, 31], +// [32] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/TraverseBreadthFirst/TraverseBreadthFirst.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TraverseBreadthFirst/TraverseBreadthFirst.linq new file mode 100644 index 000000000..6202d2af1 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TraverseBreadthFirst/TraverseBreadthFirst.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +Node root = new(0, +[ + new(1), new(2), new(3), + new(4, + [ + new(5), new(6), + new(7, [ new(8), new(9) ]), + new(10, [ new(11) ]), + ]), + new(11), + new(12, + [ + new(13), new(14), + new(15, [ new(16), new(17) ]), + new(18), + ]), + new(19), new(20), +]); + +// Traverse a tree +var result = SuperEnumerable + .TraverseBreadthFirst( + root, + static x => x.Children ?? []) + .Select(x => x.Id); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0, 1, 2, 3, 4, 11, 12, 19, 20, 5, 6, 7, 10, 13, 14, 15, 18, 8, 9, 11, 16, 17] + +record Node(int Id, IEnumerable? Children = null); diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/TraverseDepthFirst/TraverseDepthFirst.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TraverseDepthFirst/TraverseDepthFirst.linq new file mode 100644 index 000000000..9ee165246 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TraverseDepthFirst/TraverseDepthFirst.linq @@ -0,0 +1,40 @@ + + SuperLinq + SuperLinq + + +Node root = new(0, +[ + new(1), new(2), new(3), + new(4, + [ + new(5), new(6), + new(7, [ new(8), new(9) ]), + new(10, [ new(11) ]), + ]), + new(11), + new(12, + [ + new(13), new(14), + new(15, [ new(16), new(17) ]), + new(18), + ]), + new(19), new(20), +]); + +// Traverse a tree +var result = SuperEnumerable + .TraverseDepthFirst( + root, + static x => x.Children ?? []) + .Select(x => x.Id); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + +record Node(int Id, IEnumerable? Children = null); diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/TrySingle/TrySingle1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TrySingle/TrySingle1.linq new file mode 100644 index 000000000..0dcd1accd --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TrySingle/TrySingle1.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +// Determine cardinality of sequence +var result = Enumerable.Range(1, 0).TrySingle("none", "one", "many"); +Console.WriteLine(result.ToString()); + +result = Enumerable.Range(1, 1).TrySingle("none", "one", "many"); +Console.WriteLine(result.ToString()); + +result = Enumerable.Range(1, 10).TrySingle("none", "one", "many"); +Console.WriteLine(result.ToString()); + +// This code produces the following output: +// (none, 0) +// (one, 1) +// (many, 0) diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/TrySingle/TrySingle2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TrySingle/TrySingle2.linq new file mode 100644 index 000000000..4501893de --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/TrySingle/TrySingle2.linq @@ -0,0 +1,19 @@ + + SuperLinq + SuperLinq + + +// Determine cardinality of sequence +var result = Enumerable.Range(10, 0).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" }); +Console.WriteLine(result); + +result = Enumerable.Range(10, 1).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" }); +Console.WriteLine(result); + +result = Enumerable.Range(10, 10).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" }); +Console.WriteLine(result); + +// This code produces the following output: +// no elements +// single(10) +// many elements diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Using/Using.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Using/Using.linq new file mode 100644 index 000000000..7133c901f --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Using/Using.linq @@ -0,0 +1,45 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Hold a resource for the duration of an enumeration +var result = SuperEnumerable + .Using( + () => new DummyDisposable(), + d => d.GetValues()) + .Do(x => Console.Write($"{x}, ")); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// Constructor +// GetValues +// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, +// Dispose +// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + +class DummyDisposable : IDisposable +{ + public DummyDisposable() + { + Console.WriteLine("Constructor"); + } + + public IEnumerable GetValues() + { + Console.WriteLine("GetValues"); + return Enumerable.Range(1, 10); + } + + public void Dispose() + { + Console.WriteLine(); + Console.WriteLine("Dispose"); + } +} diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Where/Where.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Where/Where.linq new file mode 100644 index 000000000..491576513 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Where/Where.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 5); + +// Filter a sequence based on a matching sequence of bools +var result = sequence + .Where([true, false, true, true, false]); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 3, 4] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLag/WhereLag1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLag/WhereLag1.linq new file mode 100644 index 000000000..0f447673e --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLag/WhereLag1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Filter a sequence based on a leading value +var result = sequence + .WhereLag(1, (cur, lag) => cur + lag < 10); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLag/WhereLag2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLag/WhereLag2.linq new file mode 100644 index 000000000..9ed117b05 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLag/WhereLag2.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Filter a sequence based on a leading value +var result = sequence + .WhereLag(1, 20, (cur, lag) => cur + lag < 10); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [2, 3, 4, 5] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLead/WhereLead1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLead/WhereLead1.linq new file mode 100644 index 000000000..7445d4aca --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLead/WhereLead1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Filter a sequence based on a leading value +var result = sequence + .WhereLead(1, (cur, lead) => cur + lead >= 10); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [5, 6, 7, 8, 9, 10] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLead/WhereLead2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLead/WhereLead2.linq new file mode 100644 index 000000000..fb9010578 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WhereLead/WhereLead2.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Filter a sequence based on a leading value +var result = sequence + .WhereLead(1, -20, (cur, lead) => cur + lead >= 10); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [5, 6, 7, 8, 9] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/While/While.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/While/While.linq new file mode 100644 index 000000000..5f9f06131 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/While/While.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 2); + +// Repeat a sequence while a condition is true +var count = 0; +var result = SuperEnumerable + .While( + () => count++ < 3, + sequence); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [1, 2, 1, 2, 1, 2] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window1.linq new file mode 100644 index 000000000..585d06a14 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window1.linq @@ -0,0 +1,28 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var result = sequence.Window(3); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window2.linq new file mode 100644 index 000000000..37f5d4737 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window2.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into three chunks of 3 and one chunk of 1 +var result = sequence + .Window( + 3, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window3.linq new file mode 100644 index 000000000..fb7a77430 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window3.linq @@ -0,0 +1,32 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into three chunks of 3 and one chunk of 1 +var buffer = new int[3]; +var result = sequence + .Window( + buffer, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window4.linq new file mode 100644 index 000000000..949fcd80d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/Window/Window4.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Break the sequence of numbers into three chunks of 3 and one chunk of 1 +var buffer = new int[5]; +var result = sequence + .Window( + buffer, + 3, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft1.linq new file mode 100644 index 000000000..0f14c5c72 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft1.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var result = sequence.WindowLeft(3); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10], +// [9, 10], +// [10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft2.linq new file mode 100644 index 000000000..c96e18ba3 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft2.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var result = sequence + .WindowLeft( + 3, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10], +// [9, 10], +// [10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft3.linq new file mode 100644 index 000000000..a839e2c0c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft3.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var buffer = new int[3]; +var result = sequence + .WindowLeft( + buffer, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10], +// [9, 10], +// [10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft4.linq new file mode 100644 index 000000000..7ff8d1e45 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowLeft/WindowLeft4.linq @@ -0,0 +1,35 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var buffer = new int[5]; +var result = sequence + .WindowLeft( + buffer, + 3, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10], +// [9, 10], +// [10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight1.linq new file mode 100644 index 000000000..4aeb35610 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight1.linq @@ -0,0 +1,30 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var result = sequence.WindowRight(3); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result.Select(c => " [" + string.Join(", ", c) + "]")) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1], +// [1, 2], +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight2.linq new file mode 100644 index 000000000..e5349452a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight2.linq @@ -0,0 +1,33 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var result = sequence + .WindowRight( + 3, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1], +// [1, 2], +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight3.linq new file mode 100644 index 000000000..b5638ca6a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight3.linq @@ -0,0 +1,34 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var buffer = new int[3]; +var result = sequence + .WindowRight( + buffer, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1], +// [1, 2], +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight4.linq new file mode 100644 index 000000000..a5ac01f01 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/WindowRight/WindowRight4.linq @@ -0,0 +1,35 @@ + + SuperLinq + SuperLinq + + +var sequence = Enumerable.Range(1, 10); + +// Get a sliding window over the sequence +var buffer = new int[5]; +var result = sequence + .WindowRight( + buffer, + 3, + c => " [" + string.Join(", ", c) + "]"); + +Console.WriteLine( + "[" + Environment.NewLine + + string.Join( + ", " + Environment.NewLine, + result) + + Environment.NewLine + "]"); + +// This code produces the following output: +// [ +// [1], +// [1, 2], +// [1, 2, 3], +// [2, 3, 4], +// [3, 4, 5], +// [4, 5, 6], +// [5, 6, 7], +// [6, 7, 8], +// [7, 8, 9], +// [8, 9, 10] +// ] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest1.linq new file mode 100644 index 000000000..d1449e94a --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, 5 }; + +// Zip two sequences together +var result = seq1.ZipLongest(seq2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1), (bb, 2), (c, 3), (ddd, 4), (, 5)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest2.linq new file mode 100644 index 000000000..3ad6610f6 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest2.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, 5 }; + +// Zip two sequences together +var result = seq1 + .ZipLongest( + seq2, + (a, b) => new { A = a, B = b, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ A = aaa, B = 1 }, { A = bb, B = 2 }, { A = c, B = 3 }, { A = ddd, B = 4 }, { A = , B = 5 }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest3.linq new file mode 100644 index 000000000..4d16c03c5 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest3.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", }; +var seq2 = new[] { 1, 2, 3, 4, }; +var seq3 = new[] { 20, 5, 7, 12, 42 }; + +// Zip three sequences together +var result = seq1 + .ZipLongest( + seq2, + seq3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1, 20), (bb, 2, 5), (c, 3, 7), (, 4, 12), (, 0, 42)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest4.linq new file mode 100644 index 000000000..6dbb3d4d4 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest4.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", }; +var seq2 = new[] { 1, 2, 3, 4, }; +var seq3 = new[] { 20, 5, 7, 12, 42 }; + +// Zip three sequences together +var result = seq1 + .ZipLongest( + seq2, + seq3, + (a, b, c) => new { A = a, B = b, C = c, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ A = aaa, B = 1, C = 20 }, { A = bb, B = 2, C = 5 }, { A = c, B = 3, C = 7 }, { A = , B = 4, C = 12 }, { A = , B = 0, C = 42 }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest5.linq new file mode 100644 index 000000000..8c1afaf8d --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest5.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, }; +var seq3 = new[] { 20, 5, }; +var seq4 = new[] { "zz", }; + +// Zip four sequences together +var result = seq1 + .ZipLongest( + seq2, + seq3, + seq4); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1, 20, zz), (bb, 2, 5, ), (c, 3, 0, ), (ddd, 0, 0, )] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest6.linq new file mode 100644 index 000000000..6f86c40c1 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipLongest/ZipLongest6.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, }; +var seq3 = new[] { 20, 5, }; +var seq4 = new[] { "zz", }; + +// Zip four sequences together +var result = seq1 + .ZipLongest( + seq2, + seq3, + seq4, + (a, b, c, d) => new + { + A = a, + B = b, + C = c, + D = d, + }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ A = aaa, B = 1, C = 20, D = zz }, { A = bb, B = 2, C = 5, D = }, { A = c, B = 3, C = 0, D = }, { A = ddd, B = 0, C = 0, D = }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipMap/ZipMap.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipMap/ZipMap.linq new file mode 100644 index 000000000..a109788b7 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipMap/ZipMap.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var sequence = new[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", }; + +// Filter a sequence based on a leading value +var result = sequence + .ZipMap(x => x.ToString().Length); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(one, 3), (two, 3), (three, 5), (four, 4), (five, 4), (six, 3), (seven, 5), (eight, 5), (nine, 4), (ten, 3)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest1.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest1.linq new file mode 100644 index 000000000..7255cc1e1 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest1.linq @@ -0,0 +1,18 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, 5 }; + +// Zip two sequences together +var result = seq1.ZipShortest(seq2); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1), (bb, 2), (c, 3), (ddd, 4)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest2.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest2.linq new file mode 100644 index 000000000..b2fa5afa3 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest2.linq @@ -0,0 +1,21 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, 4, 5 }; + +// Zip two sequences together +var result = seq1 + .ZipShortest( + seq2, + (a, b) => new { A = a, B = b, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ A = aaa, B = 1 }, { A = bb, B = 2 }, { A = c, B = 3 }, { A = ddd, B = 4 }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest3.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest3.linq new file mode 100644 index 000000000..452c55352 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest3.linq @@ -0,0 +1,22 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", }; +var seq2 = new[] { 1, 2, 3, 4, }; +var seq3 = new[] { 20, 5, 7, 12, 42 }; + +// Zip three sequences together +var result = seq1 + .ZipShortest( + seq2, + seq3); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1, 20), (bb, 2, 5), (c, 3, 7)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest4.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest4.linq new file mode 100644 index 000000000..17829a230 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest4.linq @@ -0,0 +1,23 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", }; +var seq2 = new[] { 1, 2, 3, 4, }; +var seq3 = new[] { 20, 5, 7, 12, 42 }; + +// Zip three sequences together +var result = seq1 + .ZipShortest( + seq2, + seq3, + (a, b, c) => new { A = a, B = b, C = c, }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ A = aaa, B = 1, C = 20 }, { A = bb, B = 2, C = 5 }, { A = c, B = 3, C = 7 }] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest5.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest5.linq new file mode 100644 index 000000000..8faa63362 --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest5.linq @@ -0,0 +1,24 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, }; +var seq3 = new[] { 20, 5, }; +var seq4 = new[] { "zz", }; + +// Zip four sequences together +var result = seq1 + .ZipShortest( + seq2, + seq3, + seq4); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [(aaa, 1, 20, zz)] diff --git a/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest6.linq b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest6.linq new file mode 100644 index 000000000..ea7c1840c --- /dev/null +++ b/Docs/SuperLinq.Docs/apidoc/SuperLinq/ZipShortest/ZipShortest6.linq @@ -0,0 +1,31 @@ + + SuperLinq + SuperLinq + + +var seq1 = new[] { "aaa", "bb", "c", "ddd", }; +var seq2 = new[] { 1, 2, 3, }; +var seq3 = new[] { 20, 5, }; +var seq4 = new[] { "zz", }; + +// Zip four sequences together +var result = seq1 + .ZipShortest( + seq2, + seq3, + seq4, + (a, b, c, d) => new + { + A = a, + B = b, + C = c, + D = d, + }); + +Console.WriteLine( + "[" + + string.Join(", ", result) + + "]"); + +// This code produces the following output: +// [{ A = aaa, B = 1, C = 20, D = zz }] diff --git a/Source/SuperLinq/SuperLinq.csproj b/Source/SuperLinq/SuperLinq.csproj index 36538b563..8473138e6 100644 --- a/Source/SuperLinq/SuperLinq.csproj +++ b/Source/SuperLinq/SuperLinq.csproj @@ -11,7 +11,7 @@ SuperLinq SuperLinq Developers - linq;extensions + linq;extensions;linqpad-samples Apache-2.0 readme.md @@ -141,8 +141,14 @@ $([System.Text.RegularExpressions.Regex]::Replace($(Copyright), `\s+`, ` `).Trim()) - + + + + +