A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
61
Task/Generator-Exponential/D/generator-exponential-1.d
Normal file
61
Task/Generator-Exponential/D/generator-exponential-1.d
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import std.stdio, std.bigint, std.range, std.algorithm;
|
||||
|
||||
struct Filtered(R1, R2) if (is(ElementType!R1 == ElementType!R2)) {
|
||||
R1 s1;
|
||||
R2 s2;
|
||||
alias ElementType!R1 T;
|
||||
T current, source, filter;
|
||||
|
||||
this(R1 r1, R2 r2) {
|
||||
s1 = r1;
|
||||
s2 = r2;
|
||||
source = s1.front();
|
||||
filter = s2.front();
|
||||
_next();
|
||||
}
|
||||
|
||||
const bool empty = false;
|
||||
@property T front() { return current; }
|
||||
void popFront() { _next(); }
|
||||
|
||||
private void _next() {
|
||||
while (true) {
|
||||
if (source > filter) {
|
||||
s2.popFront();
|
||||
filter = s2.front();
|
||||
continue;
|
||||
} else if (source < filter) {
|
||||
current = source;
|
||||
s1.popFront();
|
||||
source = s1.front();
|
||||
break;
|
||||
}
|
||||
s1.popFront();
|
||||
source = s1.front();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto filtered(R1, R2)(R1 r1, R2 r2)
|
||||
if (is(ElementType!R1 == ElementType!R2)) {
|
||||
return Filtered!(R1, R2)(r1, r2);
|
||||
}
|
||||
|
||||
struct Count(T) {
|
||||
T n;
|
||||
const bool empty = false;
|
||||
@property T front() { return n; }
|
||||
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 = count!BigInt().map!q{a ^^ 2}();
|
||||
auto cubes = count!BigInt().map!q{a ^^ 3}();
|
||||
auto f = filtered(squares, cubes);
|
||||
|
||||
popFrontN(f, 20);
|
||||
writeln(take(f, 10));
|
||||
}
|
||||
33
Task/Generator-Exponential/D/generator-exponential-2.d
Normal file
33
Task/Generator-Exponential/D/generator-exponential-2.d
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import std.stdio, std.traits;
|
||||
|
||||
auto powers(in double e) pure nothrow {
|
||||
double i = 0.0;
|
||||
return () => i++ ^^ e;
|
||||
}
|
||||
|
||||
auto filter(D)(D af, D bf) if (isCallable!D) {
|
||||
double a = af(), b = bf();
|
||||
return {
|
||||
double r;
|
||||
while (true) {
|
||||
if (a < b) {
|
||||
r = a;
|
||||
a = af();
|
||||
break;
|
||||
}
|
||||
if (b == a)
|
||||
a = af();
|
||||
b = bf();
|
||||
}
|
||||
return r;
|
||||
};
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto fgen = filter(powers(2), powers(3));
|
||||
foreach (i; 0 .. 20)
|
||||
fgen();
|
||||
foreach (i; 0 .. 10)
|
||||
write(fgen(), " ");
|
||||
writeln();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue