What to use instead of array.join if RHS is not a range?
bearophile
bearophileHUGS at lycos.com
Mon Nov 26 18:04:40 PST 2012
Andrej Mitrovic:
> Is there some other function which can join with an
> element as a separator?
This is not efficient, but it's useful to call it inside the
post-condition of a more efficient version to test the results of
the faster version:
import std.stdio, std.array;
//
http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-List.html#intersperse
T[] intersperse(T)(T[] items, T sep) pure nothrow {
static T[] prependToAll(T[] items, T sep) pure nothrow {
return items.empty ?
[] :
sep ~ (items.front ~ prependToAll(items[1 .. $],
sep));
}
return items.empty ?
[] :
items.front ~ prependToAll(items[1 .. $], sep);
}
void main() {
static struct S { int x; }
auto arr = [S(2), S(4), S(6)];
auto s = S(10);
auto result = arr.intersperse(s);
writeln(result);
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list