Instantly share code, notes, and snippets.

Embed
What would you like to do?
Voldemort types article
module eponymous;
template inputChain(R1, R2)
if(isInputRange!R1 && isInputRange!R2 &&
is(ElementType!R1 == ElementType!R2))
{
auto inputChain(R1 r1, R2 r2)
{
...
}
}
import std.range;
auto inputChain(R1, R2)(R1 r1, R2 r2)
if(isInputRange!R1 && isInputRange!R2 &&
is(ElementType!R1 == ElementType!R2))
{
static struct Chain
{
R1 r1;
R2 r2;
auto front() { return r1.empty ? r2.front : r1.front; }
void popFront()
{
if(r1.empty)
r2.popFront;
else
r1.popFront;
}
bool empty() { return r1.empty && r2.empty; }
}
return Chain(r1, r2);
}
module simplechain;
import std.range;
auto inputChain(R1, R2)(R1 r1, R2 r2)
if(isInputRange!R1 && isInputRange!R2 &&
is(ElementType!R1 == ElementType!R2))
{
static struct Chain
{
R1 r1;
R2 r2;
auto front() { throw new Exception(""); return r1.empty ? r2.front : r1.front; }
void popFront()
{
if(r1.empty)
r2.popFront;
else
r1.popFront;
}
bool empty() { return r1.empty && r2.empty; }
}
return Chain(r1, r2);
}
module simplechain;
import std.range;
private struct Chain(R1, R2)
if(isInputRange!R1 && isInputRange!R2 &&
is(ElementType!R1 == ElementType!R2))
{
R1 r1;
R2 r2;
auto front() { throw new Exception(""); return r1.empty ? r2.front : r1.front; }
void popFront()
{
if(r1.empty)
r2.popFront;
else
r1.popFront;
}
bool empty() { return r1.empty && r2.empty; }
}
auto inputChain(R1, R2)(R1 r1, R2 r2)
if(isInputRange!R1 && isInputRange!R2 &&
is(ElementType!R1 == ElementType!R2))
{
return Chain!(R1, R2)(r1, r2); // note the explicit instantation
}
module simplechain;
import std.range;
template inputChain(R1, R2)
if(isInputRange!R1 && isInputRange!R2 &&
is(ElementType!R1 == ElementType!R2))
{
struct Chain
{
R1 r1;
R2 r2;
auto front() { throw new Exception(""); return r1.empty ? r2.front : r1.front; }
void popFront()
{
if(r1.empty)
r2.popFront;
else
r1.popFront;
}
bool empty() { return r1.empty && r2.empty; }
}
auto inputChain(R1 r1, R2 r2)
{
return Chain(r1, r2);
}
}
...
auto front() { throw new Exception(""); return r1.empty? ... }
...
import simplechain;
import std.stdio;
void main()
{
auto ch = inputChain("hello, ", "world!");
writeln(ch);
}
import simplechain;
import std.stdio;
void main()
{
auto ch = inputChain("hello ", "there, ").inputChain("world!");
writeln(ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment