Python の itertools.count を C# で作る

Pythonitertools.countC# で作ってみます。

using System;
using System.Collections.Generic;
using System.Linq;

class Iter {
    public static IEnumerable<int> Count(int start, int step=1) {
        int x = start;
        while (true) {
            yield return x;
            x += step;
        }
    }
}

class Program {
    static void Main() {
        Console.WriteLine(string.Join(",", Iter.Count(0).Take(5)));
        //=> 0,1,2,3,4

        Console.WriteLine(string.Join(",", Iter.Count(10, 2).Take(4)));
        //=> 10,12,14,16

        int[] xs = { 90, 34, -5, 123 };
        foreach (var a in xs.Zip(Iter.Count(1), (a, b) => new { a, b })) {
            Console.WriteLine("{0} {1}", a.a, a.b);
        }
    }
}

実行結果です。

0,1,2,3,4
10,12,14,16
90 1
34 2
-5 3
123 4