My solution is here:using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq; // only because we use Take in Main
public class IntegersFrom : IEnumerable<long>
{
private long low;
public IntegersFrom(long low)
{
this.low = low;
}
public long LowerLimit
{
get { return low; }
}
private class IntegerEnumerator : IEnumerator<long>
{
private IntegersFrom intfr;
private long curint;
public IntegerEnumerator(IntegersFrom intfr)
{
this.intfr = intfr;
}
public long Current { get { return curint; } }
object IEnumerator.Current { get { return curint; } } // !!
public bool MoveNext() { curint++; return true; }
public void Reset() { curint = intfr.low; }
void IDisposable.Dispose(){} // !!
}
public IEnumerator<long> GetEnumerator()
{
return new IntegerEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class IntegersFromAlternative : IEnumerable<long>
{
private long low;
public IntegersFromAlternative(long low)
{
this.low = low;
}
public long LowerLimit
{
get { return low; }
}
public IEnumerator<long> GetEnumerator(){
for(long i = low; true; i++)
yield return i;
}
IEnumerator IEnumerable.GetEnumerator() // !!
{
return GetEnumerator();
}
}
internal static class Extensions
{
public static IEnumerable<long> AdInfinitum(this long from)
{
for (long i = from; true; i++)
yield return i;
}
public static IEnumerable<long> Filter(this IEnumerable<long> source, Func<long,bool> pred)
{
foreach (long i in source)
if (pred(i)) yield return i;
}
public static IEnumerable<long> Add(this IEnumerable<long> source, IEnumerable<long> other)
{
IEnumerator<long> sourceEnum = source.GetEnumerator();
IEnumerator<long> otherEnum = other.GetEnumerator();
while (true)
{
yield return sourceEnum.Current + otherEnum.Current;
sourceEnum.MoveNext();
otherEnum.MoveNext();
}
}
}
class Program
{
static void Main(string[] args)
{
var itf1 = new IntegersFrom(1);
var itf2 = new IntegersFromAlternative(1);
// foreach (long i in itf1) { Console.WriteLine(i); }
IEnumerable<long> natNums = 1L.AdInfinitum();
// foreach (long i in natNums) { Console.WriteLine(i); }
//foreach (long i in natNums.Filter(n => n % 2 == 0))
//{
// Console.WriteLine(i);
//}
//var even100 = natNums.Filter(n => n % 2 == 0).Take(100);
//foreach (long i in even100) Console.WriteLine(i);
var evenNumbers = natNums.Add(natNums).Take(100);
foreach (long i in evenNumbers) Console.WriteLine(i);
Console.ReadLine();
}
}