Skip to content

Commit

Permalink
Fix "Unfold" bug so iterations are repeatable
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Apr 1, 2023
1 parent 93bc6dc commit 034ba56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions MoreLinq.Test/UnfoldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,18 @@ public void UnfoldEmptySequence()
e => e.Result);
Assert.That(result, Is.Empty);
}

[Test(Description = "https://github.com/morelinq/MoreLINQ/issues/990")]
public void UnfoldReiterationsReturnsSameResult()
{
var xs = MoreEnumerable.Unfold(1, n => (Result: n, Next: n + 1),
_ => true,
n => n.Next,
n => n.Result)
.Take(5);

xs.AssertSequenceEqual(1, 2, 3, 4, 5);
xs.AssertSequenceEqual(1, 2, 3, 4, 5);
}
}
}
4 changes: 2 additions & 2 deletions MoreLinq/Unfold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public static IEnumerable<TResult> Unfold<TState, T, TResult>(
if (stateSelector == null) throw new ArgumentNullException(nameof(stateSelector));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(state); IEnumerable<TResult> _(TState initialState)
{
while (true)
for (var state = initialState; ;)
{
var step = generator(state);

Expand Down

0 comments on commit 034ba56

Please sign in to comment.