Skip to content

Commit ae9eea5

Browse files
authored
Merge fix bug fix for "Unfold"
2 parents d784050 + 034ba56 commit ae9eea5

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

MoreLinq.Test/UnfoldTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,18 @@ public void UnfoldEmptySequence()
8181
e => e.Result);
8282
Assert.That(result, Is.Empty);
8383
}
84+
85+
[Test(Description = "https://github.com/morelinq/MoreLINQ/issues/990")]
86+
public void UnfoldReiterationsReturnsSameResult()
87+
{
88+
var xs = MoreEnumerable.Unfold(1, n => (Result: n, Next: n + 1),
89+
_ => true,
90+
n => n.Next,
91+
n => n.Result)
92+
.Take(5);
93+
94+
xs.AssertSequenceEqual(1, 2, 3, 4, 5);
95+
xs.AssertSequenceEqual(1, 2, 3, 4, 5);
96+
}
8497
}
8598
}

MoreLinq/Unfold.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public static IEnumerable<TResult> Unfold<TState, T, TResult>(
6161
if (stateSelector == null) throw new ArgumentNullException(nameof(stateSelector));
6262
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));
6363

64-
return _(); IEnumerable<TResult> _()
64+
return _(state); IEnumerable<TResult> _(TState initialState)
6565
{
66-
while (true)
66+
for (var state = initialState; ;)
6767
{
6868
var step = generator(state);
6969

0 commit comments

Comments
 (0)