Looking for an equivalent to C++ std::getline in D
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat May 6 01:53:12 PDT 2017
On Saturday, May 6, 2017 8:34:11 AM CEST k-five via Digitalmars-d-learn
wrote:
> On Friday, 5 May 2017 at 17:07:25 UTC, Stanislav Blinov wrote:
> > On Friday, 5 May 2017 at 09:54:03 UTC, k-five wrote:
> ======================================================
>
> Thanks.
> I only needed this part since it filters the empty elements and
> this is enough for me:
> auto input = args[1].splitter('/').filter!"!a.empty"();
>
> but if your are wailing please explain what is the type of
> "input" since I think this is an array but after testing it has
> no [] - index operator ( imput[ 0 ] is fail )
>
> Also what is the parameter "a.empty" for template filter
It's a range. The exact type is a "voldemort" type in that its declared
internally to filter, and you can't reference it by name. Rather, it has a
known API, so you don't need to name it. You just use it. But that does
require that you know about D ranges (since you can't use the API if you're
not familiar with it).
If you're not familiar with D's ranges, I would suggest reading this:
http://ddili.org/ders/d.en/ranges.html
There's also this talk from dconf 2015:
https://www.youtube.com/watch?v=A8Btr8TPJ8c
But if you don't have at least a basic understanding of what ranges are, a
good chunk of D's standard library (std.algorithm in particular) is likely
to be confusing.
Now, if you want to get a dynamic array out of a range, then you can call
std.array.array on it, and array will allocate a dynamic array. So, if you
call array on input, then you'll have a dynamic array to operate on instead.
In general though, if you're able to operate on ranges rather than dynamic
arrays specifically, the code will be more efficient (e.g. both splitter and
filter return lazy ranges, so they're not actually doing any work until you
iterate over the range, and converting the range to a dynamic array would
mean iterating through the entire range as well as allocating memory). So,
while converting to a dynamic array is often the right solution, it's
usually better to avoid it if you don't need it. But obviously, you'll need
to be familiar with ranges first.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list