0 Comments Posted in:

The nineteenth video in my exploring MoreLINQ series looks at the Generate, GenerateByIndex, Repeat, Sequence and Unfold methods.

Unlike most of the other MoreLINQ methods we've looked at so far in this series, these are designed to create new IEnumerable sequences rather than operating on existing sequences.

You can generate sequences where each element depends on the previous with Generate, or where each element depends on the index GenerateByIndex. Repeat repeats an existing sequence, while Sequence adds more capabilities to the built in LINQ Enumerable.Range method by supporting custom step values.

Finally, the Unfold method is the most powerful of them all, allowing you to maintain a state object to help you generate the next value in the sequence and decide when to stop. It allows you to do things like implement the Fibonacci sequence in a single line. The only unfortunate thing about MoreLINQ's Unfold method is that it doesn't offer simplified overloads with fewer parameters, as you don't always need the full power on offer.

So for example if we defined a helper based on the MoreLINQ Unfold method:

static IEnumerable<TResult> Unfold<TState,TResult>(TState initial, Func<TState,TState> stateSelector, Func<TState,TResult> resultSelector)
    return MoreEnumerable.Unfold(initial, s=>s, s=>true, stateSelector, resultSelector);

Then we could set up an infinite Fibonacci sequence like this:

Unfold((a:0,b:1),s => (s.b, s.a + s.b), s => s.a + s.b)

You can find all videos in this series here.

Want to learn more about LINQ? Be sure to check out my Pluralsight course More Effective LINQ.
Vote on HN