Generator/Exponential: Difference between revisions
Content added Content deleted
(→{{header|Visual Basic .NET}}: Added min compiler version) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 254: | Line 254: | ||
I: 9, F: 1024 |
I: 9, F: 1024 |
||
I: 10, F: 1089</pre> |
I: 10, F: 1089</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{trans|Python}}{{works with|ALGOL 68|Revision 1 - with [[currying]] of functions and PRAGMA READ extensions}} |
|||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.5 algol68g-2.3.5].}} |
|||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
|||
'''File: Template.Generator.a68'''<lang algol68>MODE YIELDVALUE = PROC(VALUE)VOID; |
|||
MODE GENVALUE = PROC(YIELDVALUE)VOID; |
|||
PROC gen filtered = (GENVALUE gen candidate, gen exclude, YIELDVALUE yield)VOID: ( |
|||
VALUE candidate; SEMA have next exclude = LEVEL 0; |
|||
VALUE exclude; SEMA get next exclude = LEVEL 0; |
|||
BOOL initialise exclude := TRUE; |
|||
PAR ( # run each generator in a different thread # |
|||
# Thread 1 # |
|||
# FOR VALUE next exclude IN # gen exclude( # ) DO # |
|||
## (VALUE next exclude)VOID: ( |
|||
DOWN get next exclude; exclude := next exclude; |
|||
IF candidate <= exclude THEN |
|||
UP have next exclude |
|||
ELSE |
|||
UP get next exclude |
|||
FI |
|||
# OD #)), |
|||
# Thread 2 # |
|||
# FOR VALUE next candidate IN # gen candidate( # ) DO # |
|||
## (VALUE next candidate)VOID: ( |
|||
candidate := next candidate; |
|||
IF initialise exclude ORF candidate > exclude THEN |
|||
UP get next exclude; |
|||
DOWN have next exclude; # wait for result # |
|||
initialise exclude := FALSE |
|||
FI; |
|||
IF candidate < exclude THEN |
|||
yield(candidate) |
|||
FI |
|||
# OD #)) |
|||
) |
|||
); |
|||
PROC gen slice = (GENVALUE t, VALUE start, stop, YIELDVALUE yield)VOID: ( |
|||
INT index := 0; |
|||
# FOR VALUE i IN # t( # ) DO # |
|||
## (VALUE i)VOID: ( |
|||
IF index >= stop THEN done |
|||
ELIF index >= start THEN yield(i) FI; |
|||
index +:= 1 |
|||
# OD # )); |
|||
done: SKIP |
|||
); |
|||
PROC get list = (GENVALUE gen)[]VALUE: ( |
|||
INT upb := 0; |
|||
INT ups := 2; |
|||
FLEX [ups]VALUE out; |
|||
# FOR VALUE i IN # gen( # ) DO # |
|||
## (VALUE i)VOID:( |
|||
upb +:= 1; |
|||
IF upb > ups THEN # dynamically grow the array 50% # |
|||
[ups +:= ups OVER 2]VALUE append; append[:upb-1] := out; out := append |
|||
FI; |
|||
out[upb] := i |
|||
# OD # )) |
|||
out[:upb] |
|||
); |
|||
PROC powers = (VALUE m, YIELDVALUE yield)VOID: |
|||
FOR n FROM 0 DO yield(n ** m) OD;</lang>'''File: test.Generator.a68'''<lang algol68>#!/usr/local/bin/a68g --script # |
|||
MODE VALUE = INT; |
|||
PR READ "Template.Generator.a68" PR |
|||
GENVALUE squares = powers(2,), cubes = powers(3,); |
|||
GENVALUE fil = gen filtered(squares, cubes,); |
|||
printf(($g(0)x$, get list(gen slice(fil, 20, 30, )) ))</lang> |
|||
{{out}} |
|||
<pre> |
|||
529 576 625 676 784 841 900 961 1024 1089 |
|||
</pre> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 489: | Line 569: | ||
{{Out}} |
{{Out}} |
||
<pre>{529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089}</pre> |
<pre>{529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089}</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{trans|Python}}{{works with|ALGOL 68|Revision 1 - with [[currying]] of functions and PRAGMA READ extensions}} |
|||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.5 algol68g-2.3.5].}} |
|||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
|||
'''File: Template.Generator.a68'''<lang algol68>MODE YIELDVALUE = PROC(VALUE)VOID; |
|||
MODE GENVALUE = PROC(YIELDVALUE)VOID; |
|||
PROC gen filtered = (GENVALUE gen candidate, gen exclude, YIELDVALUE yield)VOID: ( |
|||
VALUE candidate; SEMA have next exclude = LEVEL 0; |
|||
VALUE exclude; SEMA get next exclude = LEVEL 0; |
|||
BOOL initialise exclude := TRUE; |
|||
PAR ( # run each generator in a different thread # |
|||
# Thread 1 # |
|||
# FOR VALUE next exclude IN # gen exclude( # ) DO # |
|||
## (VALUE next exclude)VOID: ( |
|||
DOWN get next exclude; exclude := next exclude; |
|||
IF candidate <= exclude THEN |
|||
UP have next exclude |
|||
ELSE |
|||
UP get next exclude |
|||
FI |
|||
# OD #)), |
|||
# Thread 2 # |
|||
# FOR VALUE next candidate IN # gen candidate( # ) DO # |
|||
## (VALUE next candidate)VOID: ( |
|||
candidate := next candidate; |
|||
IF initialise exclude ORF candidate > exclude THEN |
|||
UP get next exclude; |
|||
DOWN have next exclude; # wait for result # |
|||
initialise exclude := FALSE |
|||
FI; |
|||
IF candidate < exclude THEN |
|||
yield(candidate) |
|||
FI |
|||
# OD #)) |
|||
) |
|||
); |
|||
PROC gen slice = (GENVALUE t, VALUE start, stop, YIELDVALUE yield)VOID: ( |
|||
INT index := 0; |
|||
# FOR VALUE i IN # t( # ) DO # |
|||
## (VALUE i)VOID: ( |
|||
IF index >= stop THEN done |
|||
ELIF index >= start THEN yield(i) FI; |
|||
index +:= 1 |
|||
# OD # )); |
|||
done: SKIP |
|||
); |
|||
PROC get list = (GENVALUE gen)[]VALUE: ( |
|||
INT upb := 0; |
|||
INT ups := 2; |
|||
FLEX [ups]VALUE out; |
|||
# FOR VALUE i IN # gen( # ) DO # |
|||
## (VALUE i)VOID:( |
|||
upb +:= 1; |
|||
IF upb > ups THEN # dynamically grow the array 50% # |
|||
[ups +:= ups OVER 2]VALUE append; append[:upb-1] := out; out := append |
|||
FI; |
|||
out[upb] := i |
|||
# OD # )) |
|||
out[:upb] |
|||
); |
|||
PROC powers = (VALUE m, YIELDVALUE yield)VOID: |
|||
FOR n FROM 0 DO yield(n ** m) OD;</lang>'''File: test.Generator.a68'''<lang algol68>#!/usr/local/bin/a68g --script # |
|||
MODE VALUE = INT; |
|||
PR READ "Template.Generator.a68" PR |
|||
GENVALUE squares = powers(2,), cubes = powers(3,); |
|||
GENVALUE fil = gen filtered(squares, cubes,); |
|||
printf(($g(0)x$, get list(gen slice(fil, 20, 30, )) ))</lang> |
|||
{{out}} |
|||
<pre> |
|||
529 576 625 676 784 841 900 961 1024 1089 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 833: | Line 833: | ||
1024 |
1024 |
||
1089</pre> |
1089</pre> |
||
=={{header|C sharp}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
static class Program { |
|||
static void Main() { |
|||
Func<int, IEnumerable<int>> ms = m => Infinite().Select(i => (int)Math.Pow(i, m)); |
|||
var squares = ms(2); |
|||
var cubes = ms(3); |
|||
var filtered = squares.Where(square => cubes.First(cube => cube >= square) != square); |
|||
var final = filtered.Skip(20).Take(10); |
|||
foreach (var i in final) Console.WriteLine(i); |
|||
} |
|||
static IEnumerable<int> Infinite() { |
|||
var i = 0; |
|||
while (true) yield return i++; |
|||
} |
|||
}</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 918: | Line 939: | ||
29: 1089 |
29: 1089 |
||
</pre> |
</pre> |
||
=={{header|C sharp}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
static class Program { |
|||
static void Main() { |
|||
Func<int, IEnumerable<int>> ms = m => Infinite().Select(i => (int)Math.Pow(i, m)); |
|||
var squares = ms(2); |
|||
var cubes = ms(3); |
|||
var filtered = squares.Where(square => cubes.First(cube => cube >= square) != square); |
|||
var final = filtered.Skip(20).Take(10); |
|||
foreach (var i in final) Console.WriteLine(i); |
|||
} |
|||
static IEnumerable<int> Infinite() { |
|||
var i = 0; |
|||
while (true) yield return i++; |
|||
} |
|||
}</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 2,215: | Line 2,215: | ||
Non-cubic squares (21st to 30th) : 529 576 625 676 784 841 900 961 1024 1089 |
Non-cubic squares (21st to 30th) : 529 576 625 676 784 841 900 961 1024 1089 |
||
</pre> |
</pre> |
||
=={{header|M2000 Interpreter}}== |
|||
<lang M2000 Interpreter> |
|||
Module Generator { |
|||
PowGen = Lambda (e)-> { |
|||
=lambda i=0, e -> { |
|||
i++ |
|||
=i**e |
|||
} |
|||
} |
|||
Squares=lambda PowGen=PowGen(2) ->{ |
|||
=PowGen() |
|||
} |
|||
Cubes=Lambda PowGen=PowGen(3) -> { |
|||
=PowGen() |
|||
} |
|||
Filter=Lambda z=Squares(), Squares, m, Cubes->{ |
|||
while m<Z {m=cubes()} |
|||
if z=m then z=Squares() |
|||
=z |
|||
z=Squares() |
|||
} |
|||
For i=1 to 20 : dropit=Filter() :Next i |
|||
Document doc$="Non-cubic squares (21st to 30th)" |
|||
Print doc$ |
|||
\\ a new line to doc$ |
|||
doc$={ |
|||
} |
|||
For i=1 to 10 { |
|||
f=Filter() |
|||
Print Format$("I: {0::-2}, F: {1}",i+20, f) |
|||
doc$=Format$("I: {0::-2}, F: {1}",i+20, f)+{ |
|||
} |
|||
} |
|||
Clipboard doc$ |
|||
} |
|||
Generator |
|||
</lang> |
|||
{{out}} |
|||
<pre > |
|||
Non-cubic squares (21st to 30th) |
|||
I: 21, F: 529 |
|||
I: 22, F: 576 |
|||
I: 23, F: 625 |
|||
I: 24, F: 676 |
|||
I: 25, F: 784 |
|||
I: 26, F: 841 |
|||
I: 27, F: 900 |
|||
I: 28, F: 961 |
|||
I: 29, F: 1024 |
|||
I: 30, F: 1089 |
|||
</pre > |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
Line 2,417: | Line 2,359: | ||
end |
end |
||
</lang> |
</lang> |
||
=={{header|M2000 Interpreter}}== |
|||
<lang M2000 Interpreter> |
|||
Module Generator { |
|||
PowGen = Lambda (e)-> { |
|||
=lambda i=0, e -> { |
|||
i++ |
|||
=i**e |
|||
} |
|||
} |
|||
Squares=lambda PowGen=PowGen(2) ->{ |
|||
=PowGen() |
|||
} |
|||
Cubes=Lambda PowGen=PowGen(3) -> { |
|||
=PowGen() |
|||
} |
|||
Filter=Lambda z=Squares(), Squares, m, Cubes->{ |
|||
while m<Z {m=cubes()} |
|||
if z=m then z=Squares() |
|||
=z |
|||
z=Squares() |
|||
} |
|||
For i=1 to 20 : dropit=Filter() :Next i |
|||
Document doc$="Non-cubic squares (21st to 30th)" |
|||
Print doc$ |
|||
\\ a new line to doc$ |
|||
doc$={ |
|||
} |
|||
For i=1 to 10 { |
|||
f=Filter() |
|||
Print Format$("I: {0::-2}, F: {1}",i+20, f) |
|||
doc$=Format$("I: {0::-2}, F: {1}",i+20, f)+{ |
|||
} |
|||
} |
|||
Clipboard doc$ |
|||
} |
|||
Generator |
|||
</lang> |
|||
{{out}} |
|||
<pre > |
|||
Non-cubic squares (21st to 30th) |
|||
I: 21, F: 529 |
|||
I: 22, F: 576 |
|||
I: 23, F: 625 |
|||
I: 24, F: 676 |
|||
I: 25, F: 784 |
|||
I: 26, F: 841 |
|||
I: 27, F: 900 |
|||
I: 28, F: 961 |
|||
I: 29, F: 1024 |
|||
I: 30, F: 1089 |
|||
</pre > |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Line 2,554: | Line 2,554: | ||
{{out}}<pre>[529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089]</pre> |
{{out}}<pre>[529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089]</pre> |
||
=={{header|Perl 6}}== |
|||
{{works with|rakudo|2015.12}} |
|||
As with Haskell, generators are disguised as lazy lists in Perl 6. |
|||
<lang perl6>sub powers($m) { $m XR** 0..* } |
|||
my @squares = powers(2); |
|||
my @cubes = powers(3); |
|||
sub infix:<with-out> (@orig,@veto) { |
|||
gather for @veto -> $veto { |
|||
take @orig.shift while @orig[0] before $veto; |
|||
@orig.shift if @orig[0] eqv $veto; |
|||
} |
|||
} |
|||
say (@squares with-out @cubes)[20 ..^ 20+10].join(', ');</lang> |
|||
{{out}} |
|||
<pre>529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,942: | Line 2,922: | ||
'(529 576 625 676 784 841 900 961 1024 1089) |
'(529 576 625 676 784 841 900 961 1024 1089) |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|rakudo|2015.12}} |
|||
As with Haskell, generators are disguised as lazy lists in Perl 6. |
|||
<lang perl6>sub powers($m) { $m XR** 0..* } |
|||
my @squares = powers(2); |
|||
my @cubes = powers(3); |
|||
sub infix:<with-out> (@orig,@veto) { |
|||
gather for @veto -> $veto { |
|||
take @orig.shift while @orig[0] before $veto; |
|||
@orig.shift if @orig[0] eqv $veto; |
|||
} |
|||
} |
|||
say (@squares with-out @cubes)[20 ..^ 20+10].join(', ');</lang> |
|||
{{out}} |
|||
<pre>529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 3,078: | Line 3,079: | ||
# p f.lazy.drop(20).first(10) # Ruby 2.0+</lang> |
# p f.lazy.drop(20).first(10) # Ruby 2.0+</lang> |
||
Output is the same as the above. |
Output is the same as the above. |
||
=={{header|Scala}}== |
|||
<lang scala>object Generators { |
|||
def main(args: Array[String]): Unit = { |
|||
def squares(n:Int=0):Stream[Int]=(n*n) #:: squares(n+1) |
|||
def cubes(n:Int=0):Stream[Int]=(n*n*n) #:: cubes(n+1) |
|||
def filtered(s:Stream[Int], c:Stream[Int]):Stream[Int]={ |
|||
if(s.head>c.head) filtered(s, c.tail) |
|||
else if(s.head<c.head) Stream.cons(s.head, filtered(s.tail, c)) |
|||
else filtered(s.tail, c) |
|||
} |
|||
filtered(squares(), cubes()) drop 20 take 10 print |
|||
} |
|||
}</lang> |
|||
Here is an alternative filter implementation using pattern matching. |
|||
<lang scala>def filtered2(s:Stream[Int], c:Stream[Int]):Stream[Int]=(s, c) match { |
|||
case (sh#::_, ch#::ct) if (sh>ch) => filtered2(s, ct) |
|||
case (sh#::st, ch#::_) if (sh<ch) => sh #:: filtered2(st, c) |
|||
case (_#::st, _) => filtered2(st, c) |
|||
}</lang> |
|||
{{out}} |
|||
<pre>529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089, empty</pre> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 3,145: | Line 3,122: | ||
{{out}} |
{{out}} |
||
<pre>529 576 625 676 784 841 900 961 1024 1089 </pre> |
<pre>529 576 625 676 784 841 900 961 1024 1089 </pre> |
||
=={{header|Scala}}== |
|||
<lang scala>object Generators { |
|||
def main(args: Array[String]): Unit = { |
|||
def squares(n:Int=0):Stream[Int]=(n*n) #:: squares(n+1) |
|||
def cubes(n:Int=0):Stream[Int]=(n*n*n) #:: cubes(n+1) |
|||
def filtered(s:Stream[Int], c:Stream[Int]):Stream[Int]={ |
|||
if(s.head>c.head) filtered(s, c.tail) |
|||
else if(s.head<c.head) Stream.cons(s.head, filtered(s.tail, c)) |
|||
else filtered(s.tail, c) |
|||
} |
|||
filtered(squares(), cubes()) drop 20 take 10 print |
|||
} |
|||
}</lang> |
|||
Here is an alternative filter implementation using pattern matching. |
|||
<lang scala>def filtered2(s:Stream[Int], c:Stream[Int]):Stream[Int]=(s, c) match { |
|||
case (sh#::_, ch#::ct) if (sh>ch) => filtered2(s, ct) |
|||
case (sh#::st, ch#::_) if (sh<ch) => sh #:: filtered2(st, c) |
|||
case (_#::st, _) => filtered2(st, c) |
|||
}</lang> |
|||
{{out}} |
|||
<pre>529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089, empty</pre> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<lang lisp>(define (power-seq n) |
<lang lisp>(define (power-seq n) |
||
Line 3,218: | Line 3,220: | ||
[529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089] |
[529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089] |
||
</pre> |
</pre> |
||
=={{header|Swift}}== |
|||
<lang swift>func powGen(m: Int) -> GeneratorOf<Int> { |
|||
let power = Double(m) |
|||
var cur: Double = 0 |
|||
return GeneratorOf { Int(pow(cur++, power)) } |
|||
} |
|||
var squares = powGen(2) |
|||
var cubes = powGen(3) |
|||
var nCube = cubes.next() |
|||
var filteredSqs = GeneratorOf<Int> { |
|||
for var nSq = squares.next() ;; nCube = cubes.next() { |
|||
if nCube > nSq { |
|||
return nSq |
|||
} else if nCube == nSq { |
|||
nSq = squares.next() |
|||
} |
|||
} |
|||
} |
|||
extension GeneratorOf { |
|||
func drop(n: Int) -> GeneratorOf<T> { |
|||
var g = self |
|||
for _ in 0..<n {g.next()} |
|||
return GeneratorOf{g.next()} |
|||
} |
|||
func take(n: Int) -> GeneratorOf<T> { |
|||
var (i, g) = (0, self) |
|||
return GeneratorOf{++i > n ? nil : g.next()} |
|||
} |
|||
} |
|||
for num in filteredSqs.drop(20).take(10) { |
|||
print(num) |
|||
} |
|||
//529 |
|||
//576 |
|||
//625 |
|||
//676 |
|||
//784 |
|||
//841 |
|||
//900 |
|||
//961 |
|||
//1024 |
|||
//1089</lang> |
|||
=={{header|SuperCollider}}== |
=={{header|SuperCollider}}== |
||
Line 3,319: | Line 3,272: | ||
answers: [ 529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089 ] |
answers: [ 529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089 ] |
||
</lang> |
</lang> |
||
=={{header|Swift}}== |
|||
<lang swift>func powGen(m: Int) -> GeneratorOf<Int> { |
|||
let power = Double(m) |
|||
var cur: Double = 0 |
|||
return GeneratorOf { Int(pow(cur++, power)) } |
|||
} |
|||
var squares = powGen(2) |
|||
var cubes = powGen(3) |
|||
var nCube = cubes.next() |
|||
var filteredSqs = GeneratorOf<Int> { |
|||
for var nSq = squares.next() ;; nCube = cubes.next() { |
|||
if nCube > nSq { |
|||
return nSq |
|||
} else if nCube == nSq { |
|||
nSq = squares.next() |
|||
} |
|||
} |
|||
} |
|||
extension GeneratorOf { |
|||
func drop(n: Int) -> GeneratorOf<T> { |
|||
var g = self |
|||
for _ in 0..<n {g.next()} |
|||
return GeneratorOf{g.next()} |
|||
} |
|||
func take(n: Int) -> GeneratorOf<T> { |
|||
var (i, g) = (0, self) |
|||
return GeneratorOf{++i > n ? nil : g.next()} |
|||
} |
|||
} |
|||
for num in filteredSqs.drop(20).take(10) { |
|||
print(num) |
|||
} |
|||
//529 |
|||
//576 |
|||
//625 |
|||
//676 |
|||
//784 |
|||
//841 |
|||
//900 |
|||
//961 |
|||
//1024 |
|||
//1089</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |