Sequence to extend and forget After receiving some feetback about my previous attempt to create reusable, easily extendable sequence generators I try once again to create such a framework. 

This time there is no inheritance. Just extension methods.

The new core is the Sequence class:

public class Sequence
{
    public Sequence(int count) { Count = count; }

    public int Count { get; }

    public static Sequence Generate(int count)
    {
        return new Sequence(count);
    }

    public IEnumerable&lt;T&gt; Generate&lt;T&gt;(Func&lt;IEnumerable&lt;T&gt;&gt; generator) =&gt; generator().Take(Count);  
}


By itself it doesn't do much. You need to extend it with your extensions that hook up to it and provide it with the actual generator for your sequence.

Here are some examples for the Fibonacci sequence:

static class FibonacciSequenceExtensions
{
    public static IEnumerable&lt;T&gt; Fibonacci&lt;T&gt;(this Sequence sequence, T firstTwo, T firstStep, Func&lt;T, T, T&gt; sum) 
    {
        return sequence.Generate(() =&gt; FibonacciGenerator(firstTwo, firstStep, sum));
    }

    public static IEnumerable&lt;int&gt; Fibonacci(this Sequence sequence, int firstTwo, int firstStep)
    {
        return sequence.Generate(() =&gt; FibonacciGenerator(firstTwo, firstStep, (x, y) =&gt; x + y));
    }

    public static IEnumerable&lt;TimeSpan&gt; Fibonacci(this Sequence sequence, TimeSpan firstTwo, TimeSpan firstStep)
    {
        return sequence.Generate(() =&gt; FibonacciGenerator(firstTwo, firstStep, (x, y) =&gt; x + y));
    }

    private static IEnumerable&lt;T&gt; FibonacciGenerator&lt;T&gt;(T firstTwo, T firstStep, Func&lt;T, T, T&gt; sum)
    {
        var preview = firstTwo;
        var current = sum(firstTwo, firstStep);

        yield return preview;
        yield return preview;
        yield return current;

        while (true)
        {
            var newCurrent = sum(preview, current);
            yield return newCurrent;
            preview = current;
            current = newCurrent;
        }
    }
}


Then you can use it like this:

var result = Sequence.Generate(10).Fibonacci(3, 4).ToList();


What's the purpose of this? To have only a single class for all sequences. You don't have to remember where you've implemented some sequence. You can use them all via a single class.



In case someone is interested how the final version looks like I put it here under the previous quesiton because it's the inheritance edition that I decided to keep.
The API looks ok, IMHO, except for this syntax:

Sequence.Generate(10).Generate(...); // huh?


The downside is that for every sequence, I have to write at least two methods. First, method, to generate an actual sequence (FibonacciGenerator()), second method to wire it up to sequence.Generate (Fibonacci&lt;T&gt;()). This looks like a lot of extra code for what essentially could have been:

SequenceExtensions.FibonacciGenerator(...).Take(count);


Some middle ground can be probably achieved if you use Range instead:

Enumerable.Range(0, count) //extend this
          .Fibonacci(3, 4);


This way one method should be enough in most cases. And you can either drop the Sequence class altogether or leave it as a pretty wrapper around Enumerable.Range or similar loop abstraction:

Sequence.Create(10)      //returns basic 0..9 sequence (IEnumerable&lt;int&gt;)
        .Fibonacci(3, 4) //returns Fibonacci sequence, using initial sequence as loop


I am still not convinced that all this boiler plate code is worth the effort though. But oh well. :)

P.S. I think your Fibonacci sequence implementation is a bit unconventional. There is no "step" in classical implementation:


  every number after the first two is the sum of the two preceding ones


So if I were to see Fibonacci(3, 4) in code, I would assume, that 3 and 4 are the first two numbers of the sequence, and that it will go as 3, 4, 7, 11 and not as 3, 3, 7, 10, 17 (3+3 =/= 7).