Python の itertools.repeat を C# で作る。その 2

Python の itertools.repeat を C# で作る の、その 2 です。

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

class Iter {
    public static IEnumerable<T> Repeat<T>(T obj) {
        for (;;) {
            yield return obj;
        }
    }

    public static IEnumerable<T> Repeat<T>(T obj, int times) {
        for (int i = 0; i < times; i++) {
            yield return obj;
        }
    }
}

class Program {
    static void Main() {
        var g = Iter.Repeat(10).GetEnumerator();
        for (int i = 0; i < 3; i++) {
            if (g.MoveNext()) {
                Console.WriteLine(g.Current);
            }
        }

        foreach (var x in Iter.Repeat('c').Take(5)) {
            Console.WriteLine(x);
        }

        Console.WriteLine(string.Join(" ", Iter.Repeat('c').Take(5)));

        var q = Enumerable.Range(0, 10).Zip(Iter.Repeat(2), (a, b) => Math.Pow(a, b));
        Console.WriteLine(string.Join(" ", q));
    }
}

実行結果です。

10
10
10
c
c
c
c
c
c c c c c
0 1 4 9 16 25 36 49 64 81