On 2014-12-27 09:32, Mike Parker wrote:
The proverbial straw that prompted my blog rant back then was to do with
std.string. I wanted to split a string on a specific character. So I
looked in the std.string docs for a split function. There wasn't one.
There's a 'splitLines' -- I don't recall if it existed then, but it
wouldn't have been what I was looking for anyway. So I searched the docs
and found std.array.split. I don't recall what the docs looked like
then, but now this is what we have.
pure @safe S[] split(S)(S s) if (isSomeString!S);
This above is grokkable -- it's fairly clear that S is a string and that
an array of strings is returned. Then we get these next two versions:
auto split(R, E)(R r, E delim) if (isForwardRange!R &&
is(typeof(ElementType!R.init == E.init)));
auto split(alias isTerminator, R)(R r) if (isForwardRange!R &&
is(typeof(unaryFun!isTerminator(r.front))));
Umm... all I want is to split a string on a specific character. What's
all this mess about ElementTypes and Rs and Es and unaryFuns and....
The description, "Eagerly splits s into an array, using delim as the
delimiter." suggests this is what I'm looking for. I suppose I can just
pass it a string and a character and see what happens. But, that's just
trial and errro. The docs don't help me understand it. I don't like
using functions I don't understand. Heaven help me if I find a need for
std.array.join:
ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep) if
(isInputRange!RoR && isInputRange!(Unqual!(ElementType!RoR)) &&
isInputRange!R && is(Unqual!(ElementType!(ElementType!RoR)) ==
Unqual!(ElementType!R)));
ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) if
(isInputRange!RoR && isInputRange!(Unqual!(ElementType!RoR)));
So the function to split a string on line breaks, which is
string-specific, is in std.string. The function to split a string on
whitespace, again string-specific, is in std.array. The function to
split any range on any element is in std.array. Which means I have to
think of strings not just as arrays, but as ranges, and have to
understand that some range functions are in std.range, others in
std.array and still others in std.algorithm. That means that I don't
always know where to look when I want to do something I've not done
before. Even if I do manage to find what I'm looking for, I then may
discover that it doesn't work because I want an array, but the function
returns a range and I need to convert it to an array, which means arrays
aren't really ranges like I thought and...
There's also "split" vs "splitter" and "join" vs "joiner". This doesn't
make it easier.
--
/Jacob Carlborg