Last active
May 27, 2017 01:04
-
-
Save schveiguy/21b7a54715245b8c56b8ef70bbca4080 to your computer and use it in GitHub Desktop.
Voldemort types article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module eponymous; | |
template inputChain(R1, R2) | |
if(isInputRange!R1 && isInputRange!R2 && | |
is(ElementType!R1 == ElementType!R2)) | |
{ | |
auto inputChain(R1 r1, R2 r2) | |
{ | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
auto front() { throw new Exception(""); return r1.empty? ... } | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simplechain; | |
import std.stdio; | |
void main() | |
{ | |
auto ch = inputChain("hello, ", "world!"); | |
writeln(ch); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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