On 11/27/12, H. S. Teoh <[email protected]> wrote:
> What about std.algorithm.joiner?
Same problem.
Anyway here's a quick lazy (pun intended) implementation:
import std.array;
import std.stdio;
struct MyJoiner(T)
{
T[] array;
T sep;
@property bool empty() { return array.empty; }
void popFront()
{
if (!useSep)
array.popFront();
useSep ^= 1;
}
bool useSep;
@property T front()
{
if (useSep)
return sep;
else
return array.front;
}
}
auto myJoiner(T)(T[] array, T sep)
{
return MyJoiner!T(array, sep);
}
struct S { int x; }
void main()
{
S[] arr = [S(2), S(4), S(6)];
S s = S(0);
auto range = arr.myJoiner(s);
assert(array(range) == [S(2), S(0), S(4), S(0), S(6)]);
}