This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -1,61 +1,8 @@
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);
import std.stdio, std.bigint, std.range, std.algorithm;
popFrontN(f, 20);
writeln(take(f, 10));
auto squares = 0.sequence!"n".map!(i => i.BigInt ^^ 2);
auto cubes = 0.sequence!"n".map!(i => i.BigInt ^^ 3);
squares.setDifference(cubes).drop(20).take(10).writeln;
}

View file

@ -1,33 +1,12 @@
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();
import std.stdio, std.bigint, std.range, std.algorithm;
auto squares = 0.sequence!"n".map!(i => i.BigInt ^^ 2);
auto cubes = 0.sequence!"n".map!(i => i.BigInt ^^ 3);
squares
.filter!(s => cubes.find!(c => c >= s).front != s)
.drop(20)
.take(10)
.writeln;
}

View file

@ -0,0 +1,47 @@
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 front, source, filter;
this(R1 r1, R2 r2) {
s1 = r1;
s2 = r2;
source = s1.front;
filter = s2.front;
popFront;
}
static immutable empty = false;
void popFront() {
while (true) {
if (source > filter) {
s2.popFront;
filter = s2.front;
continue;
} else if (source < filter) {
front = source;
s1.popFront;
source = s1.front;
break;
}
s1.popFront;
source = s1.front;
}
}
}
auto filtered(R1, R2)(R1 r1, R2 r2) // Helper function.
if (isInputRange!R1 && isInputRange!R2 &&
is(ElementType!R1 == ElementType!R2)) {
return Filtered!(R1, R2)(r1, r2);
}
void main() {
auto squares = 0.sequence!"n".map!(i => i.BigInt ^^ 2);
auto cubes = 0.sequence!"n".map!(i => i.BigInt ^^ 3);
filtered(squares, cubes).drop(20).take(10).writeln;
}

View file

@ -0,0 +1,34 @@
import std.stdio;
auto powers(in double e) pure nothrow {
double i = 0;
return () => i++ ^^ e;
}
auto filter2(D)(D af, D bf) {
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 = filter2(2.powers, 3.powers);
foreach (immutable i; 0 .. 20)
fgen();
foreach (immutable i; 0 .. 10)
write(fgen(), " ");
writeln;
}