Generator/Exponential: Difference between revisions

Content deleted Content added
Peter (talk | contribs)
added Fantom example
Updated both D entries
Line 686:
 
=={{header|D}}==
===Ranges-Based version:Version===
<lang d>import std.stdio, std.bigint, std.range, std.algorithm;
 
struct Filtered(R1, R2) if (is(ElementType!R1 == ElementType!R2)) {
if (is(ElementType!R1 == ElementType!R2)) {
R1 s1;
R2 s2;
Line 705 ⟶ 704:
 
const bool empty = false;
@property T front() { return current; }
void popFront() { _next(); }
 
Line 731 ⟶ 730:
}
 
struct PowersCount(T) {
intT mn;
BigIntthis(T n;n_) //{ this.n = 0n_; }
const bool empty = false;
BigInt@property T front() { return n ^^ m; }
void popFront() { /* n++; */ n += 1; }
}
 
Count!T count(T)(T start) { return Count!T(start); }
Count!T count(T)() { return Count!T(cast(T)0); }
 
void main() {
auto squares = Powerscount!BigInt().map!q{a ^^ 2}();
auto cubes = Powerscount!BigInt().map!q{a ^^ 3}();
auto f = filtered(squares, cubes);
 
popFrontN(f, 20);
writeln(take(f, 10));
}</lang>
{{out}}
Output:
<pre>[529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089]</pre>
===Closures-Based Version===
From the Go version:
{{trans|Go}}
<lang d>import std.stdio;
 
auto powers(in double e) pure nothrow {
double i = 0.0;
return {() return=> i++ ^^ e; };
}
 
Line 782 ⟶ 786:
writeln();
}</lang>
{{out}}
Output:
<pre>529 576 625 676 784 841 900 961 1024 1089 </pre>