Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Step extension #788

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Add Step extension #788

wants to merge 3 commits into from

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