Generator/Exponential: Difference between revisions
Content added Content deleted
m (→{{header|ALGOL 68}}: wikilink currying) |
|||
Line 662: | Line 662: | ||
</pre> |
</pre> |
||
using System; |
|||
=={{header|C sharp}}== |
|||
using System.Collections.Generic; |
|||
Note that types have been made static and variables made explicit as an aid to understanding. |
|||
using System.Linq; |
|||
'''Closure On Function Style''' |
|||
static class Program { |
|||
<lang csharp>using System; |
|||
static void Main() { |
|||
using System.Collections.Generic; |
|||
Func<int, IEnumerable<int>> ms = m => Infinite().Select(i => (int)Math.Pow(i, m)); |
|||
using System.Linq; |
|||
var squares = ms(2); |
|||
var cubes = ms(3); |
|||
namespace RosettaGenerator |
|||
var filtered = squares.Where(square => cubes.First(cube => cube >= square) != square); |
|||
{ |
|||
var final = filtered.Skip(20).Take(10); |
|||
class ClosureStyle |
|||
foreach (var i in final) |
|||
{ |
|||
Console.WriteLine(i); |
|||
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; |
|||
} |
} |
||
static IEnumerable<int> Infinite() { |
|||
} |
|||
var i = 0; |
|||
} |
|||
while (true) yield return i++; |
|||
}</lang> |
|||
'''Coroutine As List Style''' |
|||
<lang csharp>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(); |
|||
} |
|||
} |
} |
||
} |
|||
}</lang> |
|||
Output (for either): |
|||
<pre>529 576 625 676 784 841 900 961 1024 1089</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |