What to use instead of array.join if RHS is not a range?

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Nov 27 03:09:43 PST 2012


On 11/27/12, H. S. Teoh <hsteoh at quickfur.ath.cx> 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)]);
}


More information about the Digitalmars-d-learn mailing list