September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,46 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace RosettaGenerator
|
||||
{
|
||||
class ClosureStyle
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Func<int> squaresGenerator = PowerGeneratorAsClosure(2);
|
||||
Func<int> cubesGenerator = PowerGeneratorAsClosure(3);
|
||||
|
||||
Func<int> filter = FilterAsClosure(squaresGenerator, cubesGenerator);
|
||||
|
||||
foreach (int i in Enumerable.Range(0, 20))
|
||||
filter();
|
||||
foreach (int i in Enumerable.Range(21, 10))
|
||||
Console.Write(filter() + " ");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public static Func<int> PowerGeneratorAsClosure(int exponent)
|
||||
{
|
||||
int x = 0;
|
||||
return () => { return (int)Math.Pow(x++, exponent); };
|
||||
}
|
||||
|
||||
public static Func<int> FilterAsClosure(Func<int> squaresGenerator, Func<int> cubesGenerator)
|
||||
{
|
||||
int squareValue;
|
||||
int cubeValue = cubesGenerator();
|
||||
return () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
squareValue = squaresGenerator();
|
||||
while (squareValue > cubeValue)
|
||||
cubeValue = cubesGenerator();
|
||||
if (squareValue < cubeValue)
|
||||
return squareValue;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace RosettaGenerator
|
||||
{
|
||||
class ListStyle
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
IEnumerator<int> squaresGenerator = PowerGeneratorAsList(2);
|
||||
IEnumerator<int> cubesGenerator = PowerGeneratorAsList(3);
|
||||
|
||||
IEnumerable<int> filter = FilterAsList(squaresGenerator, cubesGenerator);
|
||||
|
||||
foreach (int value in filter.Skip(20).Take(10))
|
||||
Console.Write(value + " ");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public static IEnumerator<int> PowerGeneratorAsList(int exponent)
|
||||
{
|
||||
int x = 0;
|
||||
while (true)
|
||||
yield return (int)Math.Pow(x++, exponent);
|
||||
}
|
||||
|
||||
public static IEnumerable<int> FilterAsList(IEnumerator<int> squaresGenerator, IEnumerator<int> cubesGenerator)
|
||||
{
|
||||
int squareValue;
|
||||
int cubeValue = cubesGenerator.Current;
|
||||
while (true)
|
||||
{
|
||||
squareValue = squaresGenerator.Current;
|
||||
while (squareValue > cubeValue)
|
||||
{
|
||||
cubesGenerator.MoveNext();
|
||||
cubeValue = cubesGenerator.Current;
|
||||
}
|
||||
if (squareValue < cubeValue)
|
||||
yield return squareValue;
|
||||
squaresGenerator.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue