Skip to content

anonymous /Program.cs

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Functional Squares program
class Program
{
static void Main(string[] args)
{
foreach (int value in SquareOf(Integers()).Take(25))
{
Console.Out.WriteLine(value);
}
}
private static IEnumerable<int> SquareOf(IEnumerable<int> integers)
{
return new SquareEnumerable(integers);
}
static IEnumerable<int> Integers()
{
return new IntegerEnumerable();
}
class SquareEnumerable : IEnumerable<int>
{
IEnumerable<int> _source;
public SquareEnumerable(IEnumerable<int> source)
{
_source = source;
}
public IEnumerator<int> GetEnumerator()
{
return new SquareEnumerator(_source);
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
class SquareEnumerator : IEnumerator<int>
{
private IEnumerator<int> _source;
public SquareEnumerator(IEnumerable<int> source)
{
_source = source.GetEnumerator();
}
public int Current
{
get { return _source.Current * _source.Current; }
}
public void Dispose() { }
object IEnumerator.Current
{
get { return Current; }
}
public bool MoveNext()
{
return _source.MoveNext();
}
public void Reset()
{
_source.Reset();
}
}
class IntegerEnumerable : IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
{
return new IntegerEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return new IntegerEnumerator();
}
}
class IntegerEnumerator : IEnumerator<int>
{
private int _counter;
public int Current
{
get { return _counter; }
}
object IEnumerator.Current
{
get { return _counter; }
}
public void Dispose() { }
public bool MoveNext()
{
if (_counter == Int32.MaxValue) return false;
_counter++;
return true;
}
public void Reset()
{
_counter = 0;
}
}
static IEnumerator<int> Integers2()
{
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.