Skip to content

Conversation

atifaziz
Copy link
Member

@atifaziz atifaziz commented Jan 8, 2021

This PR adds an extension that iterates or steps through a sequence based on another sequence of integral steps to take between elements. In other words, an integral sequence of steps drives when the iterator of the first sequence is moved. An example below shows how this could be used to generate a random walk through a sequence:

var randomWalk =
    MoreEnumerable.Unfold(new Random(), r => (Random: r, Step: r.Next(2)), _ => true, s => s.Random, s => s.Step);
var result =
    from e in Enumerable.Range(1, 10).Step(randomWalk)
    select $"{(e.Step > 0 ? "Moved" : "Still")}: {e.Item}";
foreach (var s in result)
    Console.WriteLine(s.ToString());

Sample output:

Moved: 1
Moved: 2
Still: 2
Moved: 3
Moved: 4
Moved: 5
Still: 5
Moved: 6
Still: 6
Moved: 7
Still: 7
Moved: 8
Moved: 9
Still: 9
Moved: 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant