From 5938c8a7d94cbb906b05cc86a952489c372c93bb Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Sat, 14 Oct 2023 20:20:04 +0200 Subject: [PATCH 1/8] Add simpler "Unfold" overload --- .../PublicAPI/net6.0/PublicAPI.Unshipped.txt | 1 + .../netstandard2.0/PublicAPI.Unshipped.txt | 1 + .../netstandard2.1/PublicAPI.Unshipped.txt | 1 + MoreLinq/Unfold.cs | 34 +++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt index 8959c7eec..d059d94e8 100644 --- a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt @@ -16,3 +16,4 @@ static MoreLinq.MoreEnumerable.SkipLast(System.Collections.Generic.IEnumerabl static MoreLinq.MoreEnumerable.TakeLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index 056e1801b..35eb548ff 100644 --- a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -4,3 +4,4 @@ *REMOVED*static MoreLinq.MoreEnumerable.Prepend(this System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.Append(System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.Prepend(System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt index b025e7aed..46ad748c7 100644 --- a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt @@ -12,3 +12,4 @@ static MoreLinq.MoreEnumerable.SkipLast(System.Collections.Generic.IEnumerabl static MoreLinq.MoreEnumerable.TakeLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/Unfold.cs b/MoreLinq/Unfold.cs index d6f5b8738..7e7d002aa 100644 --- a/MoreLinq/Unfold.cs +++ b/MoreLinq/Unfold.cs @@ -22,6 +22,40 @@ namespace MoreLinq static partial class MoreEnumerable { + /// + /// Returns a sequence generated by applying a state to the generator function, which + /// optionally determines the next state and element of the sequence. + /// + /// Type of state elements. + /// The type of the elements of the result sequence. + /// The initial state. + /// + /// Function that takes a state and computes a triplet of Boolean, the next state and + /// element of the sequence. The Boolean value indicates whether the sequence should + /// continue or not. If false, the sequence ends and the second (next state) and + /// third (next element) items of the triplet are ignored. + /// + /// + /// A sequence containing the results generated by the + /// function. + /// + /// This operator uses deferred execution and streams its results. + /// + /// + /// s <= 100 ? (true, s * 2, s) : + /// default); ]]> + /// The result will be a sequence that will yield { 1, 2, 4, 8, 16, 32, 64 }. + /// + + public static IEnumerable Unfold( + TState state, + Func generator) + { + if (generator == null) throw new ArgumentNullException(nameof(generator)); + + return Unfold(state, generator, e => e.Continue, e => e.State, e => e.Result); + } + /// /// Returns a sequence generated by applying a state to the generator function, /// and from its result, determines if the sequence should have a next element, its value, From a2a9721efa8249cce12e6d57925bf4865b265e17 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Sun, 15 Oct 2023 10:08:18 +0200 Subject: [PATCH 2/8] Update with a more involved example --- MoreLinq/Unfold.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/MoreLinq/Unfold.cs b/MoreLinq/Unfold.cs index 7e7d002aa..b3f07b028 100644 --- a/MoreLinq/Unfold.cs +++ b/MoreLinq/Unfold.cs @@ -42,9 +42,13 @@ static partial class MoreEnumerable /// This operator uses deferred execution and streams its results. /// /// - /// s <= 100 ? (true, s * 2, s) : - /// default); ]]> - /// The result will be a sequence that will yield { 1, 2, 4, 8, 16, 32, 64 }. + /// s.Curr < 100 ? (true, (s.Next, s.Curr + s.Next), s.Curr) : default); + /// ]]> + /// The result will be a sequence that will yield + /// { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }. /// public static IEnumerable Unfold( From 0f59db9dbfec28400638338916cec3cdc3738e22 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Thu, 19 Oct 2023 17:52:47 +0200 Subject: [PATCH 3/8] Add unit tests --- MoreLinq.Test/UnfoldTest.cs | 93 ++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 32 deletions(-) diff --git a/MoreLinq.Test/UnfoldTest.cs b/MoreLinq.Test/UnfoldTest.cs index ef2a3ee1b..6a00029a6 100644 --- a/MoreLinq.Test/UnfoldTest.cs +++ b/MoreLinq.Test/UnfoldTest.cs @@ -17,80 +17,109 @@ namespace MoreLinq.Test { + using System.Collections.Generic; + using System.Runtime.CompilerServices; using NUnit.Framework; [TestFixture] public class UnfoldTest { - [Test] - public void UnfoldInfiniteSequence() - { - var result = MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1), + static IEnumerable + TestCaseData(IEnumerable source1, + IEnumerable source2, + [CallerMemberName] string? callerMemberName = null) => + new TestCaseData[] + { + new(source1) { TestName = $"{callerMemberName}(generator)" }, + new(source2) { TestName = $"{callerMemberName}(state, generator, predicate, stateSelector, resultSelector)" }, + }; + + static IEnumerable UnfoldInfiniteSequenceData() => + TestCaseData(MoreEnumerable.Unfold(1, x => (true, State: x + 1, Result: x)), + MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1), _ => true, e => e.State, - e => e.Result) - .Take(100); + e => e.Result)); + [Test] + [TestCaseSource(nameof(UnfoldInfiniteSequenceData))] + public void UnfoldInfiniteSequence(IEnumerable unfold) + { + var result = unfold.Take(100); var expectations = MoreEnumerable.Generate(1, x => x + 1).Take(100); - Assert.That(result, Is.EqualTo(expectations)); } - [Test] - public void UnfoldFiniteSequence() - { - var result = MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1), + static IEnumerable UnfoldFiniteSequenceData() => + TestCaseData(MoreEnumerable.Unfold(1, x => x <= 100 ? (true, State: x + 1, Result: x) : default), + MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1), e => e.Result <= 100, e => e.State, - e => e.Result); + e => e.Result)); + [Test] + [TestCaseSource(nameof(UnfoldFiniteSequenceData))] + public void UnfoldFiniteSequence(IEnumerable unfold) + { + var result = unfold; var expectations = MoreEnumerable.Generate(1, x => x + 1).Take(100); - Assert.That(result, Is.EqualTo(expectations)); } [Test] public void UnfoldIsLazy() { + _ = MoreEnumerable.Unfold(0, BreakingFunc.Of()); + _ = MoreEnumerable.Unfold(0, BreakingFunc.Of(), BreakingFunc.Of<(int, int), bool>(), BreakingFunc.Of<(int, int), int>(), BreakingFunc.Of<(int, int), int>()); } - - [Test] - public void UnfoldSingleElementSequence() - { - var result = MoreEnumerable.Unfold(0, x => (Result: x, State: x + 1), + static IEnumerable UnfoldSingleElementSequenceData() => + TestCaseData(MoreEnumerable.Unfold(0, x => x == 0 ? (true, State: x + 1, Result: x) : default), + MoreEnumerable.Unfold(0, x => (Result: x, State: x + 1), x => x.Result == 0, e => e.State, - e => e.Result); + e => e.Result)); + [Test] + [TestCaseSource(nameof(UnfoldSingleElementSequenceData))] + public void UnfoldSingleElementSequence(IEnumerable unfold) + { + var result = unfold; var expectations = new[] { 0 }; - Assert.That(result, Is.EqualTo(expectations)); } - [Test] - public void UnfoldEmptySequence() - { - var result = MoreEnumerable.Unfold(0, x => (Result: x, State: x + 1), + static IEnumerable UnfoldEmptySequenceData() => + TestCaseData(MoreEnumerable.Unfold(0, x => x < 0 ? (true, State: x + 1, Result: x) : default), + MoreEnumerable.Unfold(0, x => (Result: x, State: x + 1), x => x.Result < 0, e => e.State, - e => e.Result); + e => e.Result)); + + [Test] + [TestCaseSource(nameof(UnfoldEmptySequenceData))] + public void UnfoldEmptySequence(IEnumerable unfold) + { + var result = unfold; Assert.That(result, Is.Empty); } + static IEnumerable UnfoldReiterationsReturnsSameResultData() => + TestCaseData(MoreEnumerable.Unfold(1, n => (true, State: n + 1, Result: n)), + MoreEnumerable.Unfold(1, n => (Result: n, Next: n + 1), + _ => true, + n => n.Next, + n => n.Result)); + [Test(Description = "https://github.com/morelinq/MoreLINQ/issues/990")] - public void UnfoldReiterationsReturnsSameResult() + [TestCaseSource(nameof(UnfoldReiterationsReturnsSameResultData))] + public void UnfoldReiterationsReturnsSameResult(IEnumerable unfold) { - var xs = MoreEnumerable.Unfold(1, n => (Result: n, Next: n + 1), - _ => true, - n => n.Next, - n => n.Result) - .Take(5); - + var xs = unfold.Take(5); xs.AssertSequenceEqual(1, 2, 3, 4, 5); xs.AssertSequenceEqual(1, 2, 3, 4, 5); } From 48d3560af5bb215d698df00433891d86564e0e0d Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Thu, 19 Oct 2023 17:57:28 +0200 Subject: [PATCH 4/8] Swap result and state order --- MoreLinq.Test/UnfoldTest.cs | 10 +++++----- MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt | 2 +- .../PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt | 2 +- .../PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt | 2 +- MoreLinq/Unfold.cs | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/MoreLinq.Test/UnfoldTest.cs b/MoreLinq.Test/UnfoldTest.cs index 6a00029a6..ae1fcc278 100644 --- a/MoreLinq.Test/UnfoldTest.cs +++ b/MoreLinq.Test/UnfoldTest.cs @@ -35,7 +35,7 @@ static IEnumerable }; static IEnumerable UnfoldInfiniteSequenceData() => - TestCaseData(MoreEnumerable.Unfold(1, x => (true, State: x + 1, Result: x)), + TestCaseData(MoreEnumerable.Unfold(1, x => (true, Result: x, State: x + 1)), MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1), _ => true, e => e.State, @@ -51,7 +51,7 @@ public void UnfoldInfiniteSequence(IEnumerable unfold) } static IEnumerable UnfoldFiniteSequenceData() => - TestCaseData(MoreEnumerable.Unfold(1, x => x <= 100 ? (true, State: x + 1, Result: x) : default), + TestCaseData(MoreEnumerable.Unfold(1, x => x <= 100 ? (true, Result: x, State: x + 1) : default), MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1), e => e.Result <= 100, e => e.State, @@ -78,7 +78,7 @@ public void UnfoldIsLazy() } static IEnumerable UnfoldSingleElementSequenceData() => - TestCaseData(MoreEnumerable.Unfold(0, x => x == 0 ? (true, State: x + 1, Result: x) : default), + TestCaseData(MoreEnumerable.Unfold(0, x => x == 0 ? (true, Result: x, State: x + 1) : default), MoreEnumerable.Unfold(0, x => (Result: x, State: x + 1), x => x.Result == 0, e => e.State, @@ -94,7 +94,7 @@ public void UnfoldSingleElementSequence(IEnumerable unfold) } static IEnumerable UnfoldEmptySequenceData() => - TestCaseData(MoreEnumerable.Unfold(0, x => x < 0 ? (true, State: x + 1, Result: x) : default), + TestCaseData(MoreEnumerable.Unfold(0, x => x < 0 ? (true, Result: x, State: x + 1) : default), MoreEnumerable.Unfold(0, x => (Result: x, State: x + 1), x => x.Result < 0, e => e.State, @@ -109,7 +109,7 @@ public void UnfoldEmptySequence(IEnumerable unfold) } static IEnumerable UnfoldReiterationsReturnsSameResultData() => - TestCaseData(MoreEnumerable.Unfold(1, n => (true, State: n + 1, Result: n)), + TestCaseData(MoreEnumerable.Unfold(1, n => (true, Result: n, State: n + 1)), MoreEnumerable.Unfold(1, n => (Result: n, Next: n + 1), _ => true, n => n.Next, diff --git a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt index d059d94e8..f9c225b66 100644 --- a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt @@ -16,4 +16,4 @@ static MoreLinq.MoreEnumerable.SkipLast(System.Collections.Generic.IEnumerabl static MoreLinq.MoreEnumerable.TakeLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! -static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index 35eb548ff..f44b2ddce 100644 --- a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -4,4 +4,4 @@ *REMOVED*static MoreLinq.MoreEnumerable.Prepend(this System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.Append(System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.Prepend(System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! -static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt index 46ad748c7..b96db4d11 100644 --- a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt @@ -12,4 +12,4 @@ static MoreLinq.MoreEnumerable.SkipLast(System.Collections.Generic.IEnumerabl static MoreLinq.MoreEnumerable.TakeLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! -static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/Unfold.cs b/MoreLinq/Unfold.cs index b3f07b028..6a2f46478 100644 --- a/MoreLinq/Unfold.cs +++ b/MoreLinq/Unfold.cs @@ -53,7 +53,7 @@ static partial class MoreEnumerable public static IEnumerable Unfold( TState state, - Func generator) + Func generator) { if (generator == null) throw new ArgumentNullException(nameof(generator)); From d325375208a5a4580db581ef0f986f335c686f5a Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Thu, 19 Oct 2023 18:14:01 +0200 Subject: [PATCH 5/8] Fix example per "swap result and state order" --- MoreLinq/Unfold.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq/Unfold.cs b/MoreLinq/Unfold.cs index 6a2f46478..ab1f6b364 100644 --- a/MoreLinq/Unfold.cs +++ b/MoreLinq/Unfold.cs @@ -45,7 +45,7 @@ static partial class MoreEnumerable /// s.Curr < 100 ? (true, (s.Next, s.Curr + s.Next), s.Curr) : default); + /// s => s.Curr < 100 ? (true, s.Curr, (s.Next, s.Curr + s.Next)) : default); /// ]]> /// The result will be a sequence that will yield /// { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }. From 50fe93049e2cfbe7307c90899d14dfc82655d697 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 20 Oct 2023 18:46:46 +0200 Subject: [PATCH 6/8] Put result & state into own, nested, tuple --- MoreLinq.Test/UnfoldTest.cs | 12 ++++++------ MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt | 2 +- .../PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt | 2 +- .../PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt | 2 +- MoreLinq/Unfold.cs | 12 ++++++------ 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/MoreLinq.Test/UnfoldTest.cs b/MoreLinq.Test/UnfoldTest.cs index ae1fcc278..01ccfc16b 100644 --- a/MoreLinq.Test/UnfoldTest.cs +++ b/MoreLinq.Test/UnfoldTest.cs @@ -35,7 +35,7 @@ static IEnumerable }; static IEnumerable UnfoldInfiniteSequenceData() => - TestCaseData(MoreEnumerable.Unfold(1, x => (true, Result: x, State: x + 1)), + TestCaseData(MoreEnumerable.Unfold(1, x => (true, (Result: x, State: x + 1))), MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1), _ => true, e => e.State, @@ -51,7 +51,7 @@ public void UnfoldInfiniteSequence(IEnumerable unfold) } static IEnumerable UnfoldFiniteSequenceData() => - TestCaseData(MoreEnumerable.Unfold(1, x => x <= 100 ? (true, Result: x, State: x + 1) : default), + TestCaseData(MoreEnumerable.Unfold(1, x => x <= 100 ? (true, (Result: x, State: x + 1)) : default), MoreEnumerable.Unfold(1, x => (Result: x, State: x + 1), e => e.Result <= 100, e => e.State, @@ -69,7 +69,7 @@ public void UnfoldFiniteSequence(IEnumerable unfold) [Test] public void UnfoldIsLazy() { - _ = MoreEnumerable.Unfold(0, BreakingFunc.Of()); + _ = MoreEnumerable.Unfold(0, BreakingFunc.Of()); _ = MoreEnumerable.Unfold(0, BreakingFunc.Of(), BreakingFunc.Of<(int, int), bool>(), @@ -78,7 +78,7 @@ public void UnfoldIsLazy() } static IEnumerable UnfoldSingleElementSequenceData() => - TestCaseData(MoreEnumerable.Unfold(0, x => x == 0 ? (true, Result: x, State: x + 1) : default), + TestCaseData(MoreEnumerable.Unfold(0, x => x == 0 ? (true, (Result: x, State: x + 1)) : default), MoreEnumerable.Unfold(0, x => (Result: x, State: x + 1), x => x.Result == 0, e => e.State, @@ -94,7 +94,7 @@ public void UnfoldSingleElementSequence(IEnumerable unfold) } static IEnumerable UnfoldEmptySequenceData() => - TestCaseData(MoreEnumerable.Unfold(0, x => x < 0 ? (true, Result: x, State: x + 1) : default), + TestCaseData(MoreEnumerable.Unfold(0, x => x < 0 ? (true, (Result: x, State: x + 1)) : default), MoreEnumerable.Unfold(0, x => (Result: x, State: x + 1), x => x.Result < 0, e => e.State, @@ -109,7 +109,7 @@ public void UnfoldEmptySequence(IEnumerable unfold) } static IEnumerable UnfoldReiterationsReturnsSameResultData() => - TestCaseData(MoreEnumerable.Unfold(1, n => (true, Result: n, State: n + 1)), + TestCaseData(MoreEnumerable.Unfold(1, n => (true, (Result: n, State: n + 1))), MoreEnumerable.Unfold(1, n => (Result: n, Next: n + 1), _ => true, n => n.Next, diff --git a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt index f9c225b66..eaae50606 100644 --- a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt @@ -16,4 +16,4 @@ static MoreLinq.MoreEnumerable.SkipLast(System.Collections.Generic.IEnumerabl static MoreLinq.MoreEnumerable.TakeLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! -static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index f44b2ddce..772c232e1 100644 --- a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -4,4 +4,4 @@ *REMOVED*static MoreLinq.MoreEnumerable.Prepend(this System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.Append(System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.Prepend(System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! -static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt index b96db4d11..45aeb625b 100644 --- a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt @@ -12,4 +12,4 @@ static MoreLinq.MoreEnumerable.SkipLast(System.Collections.Generic.IEnumerabl static MoreLinq.MoreEnumerable.TakeLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! -static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Unfold(TState state, System.Func! generator) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/Unfold.cs b/MoreLinq/Unfold.cs index ab1f6b364..050518a51 100644 --- a/MoreLinq/Unfold.cs +++ b/MoreLinq/Unfold.cs @@ -30,10 +30,10 @@ static partial class MoreEnumerable /// The type of the elements of the result sequence. /// The initial state. /// - /// Function that takes a state and computes a triplet of Boolean, the next state and - /// element of the sequence. The Boolean value indicates whether the sequence should - /// continue or not. If false, the sequence ends and the second (next state) and - /// third (next element) items of the triplet are ignored. + /// Function that takes a state and computes a Boolean coupled with the element of the + /// sequence to yield and the next state. The Boolean value indicates whether the sequence + /// should continue or not. If false, the sequence ends and the remaining bits, like + /// element and next state are ignored. /// /// /// A sequence containing the results generated by the @@ -53,11 +53,11 @@ static partial class MoreEnumerable public static IEnumerable Unfold( TState state, - Func generator) + Func generator) { if (generator == null) throw new ArgumentNullException(nameof(generator)); - return Unfold(state, generator, e => e.Continue, e => e.State, e => e.Result); + return Unfold(state, generator, e => e.Continue, e => e.Generated.State, e => e.Generated.Result); } /// From 09b30f04680598b2c8211554246870543f10e99d Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 20 Oct 2023 18:48:45 +0200 Subject: [PATCH 7/8] Update overload count in read-me doc --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2c9962eda..a041e24a6 100644 --- a/README.md +++ b/README.md @@ -682,6 +682,8 @@ Returns a sequence generated by applying a state to the generator function, and from its result, determines if the sequence should have a next element and its value, and the next state in the recursive call. +This method has 2 overloads. + ### Window Processes a sequence into a series of subsequences representing a windowed From 4fcd56f5f77b197192e7970b0e4d84cf78c4af17 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 20 Oct 2023 19:04:41 +0200 Subject: [PATCH 8/8] Fix var reference in example doc --- MoreLinq/Unfold.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq/Unfold.cs b/MoreLinq/Unfold.cs index 050518a51..3a7123335 100644 --- a/MoreLinq/Unfold.cs +++ b/MoreLinq/Unfold.cs @@ -47,7 +47,7 @@ static partial class MoreEnumerable /// MoreEnumerable.Unfold((Curr: 0, Next: 1), /// s => s.Curr < 100 ? (true, s.Curr, (s.Next, s.Curr + s.Next)) : default); /// ]]> - /// The result will be a sequence that will yield + /// The fibonacciNumbersLowerThan100 will be a sequence that will yield /// { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }. ///