October 03, 2014
On Friday, 3 October 2014 at 17:46:18 UTC, monarch_dodra wrote:
> I threw this together. I left out checks for infinity in favor of brevity:

Could you please take a look again at

https://github.com/nordlow/justd/blob/master/range_ex.d#L15

I added all the stuff we talked about.

Note that I had to tweak empty() a bit. I don't know if I got right. Could you check?

Further I can't get string support to work:

    writefln("%(%s\n%)", slidingSplitter("Nordlöw"));

errors as

std.format.FormatException@/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/format.d(2591): Expected '%s' format specifier for type 'SlidingSplitter!string'

and

    foreach (e; name)
    {
        version(show) writeln(e);
    }

errors as

range_ex.d(132,5): Error: invalid foreach aggregate name, define opApply(), range primitives, or use .tupleof

which diagnostics I btw is responsible for :)

Could you please check?
October 03, 2014
On Friday, 3 October 2014 at 17:46:18 UTC, monarch_dodra wrote:
>         writefln("%(%s\n%)", slidingSplitter("Nordlöw"));

That's a really cool syntax, btw. I got to remember that.
October 03, 2014
On Friday, 3 October 2014 at 20:15:33 UTC, Nordlöw wrote:
> Could you please take a look again at

I made another update at

https://github.com/nordlow/justd/blob/master/range_ex.d#L15

I had forgotten to move front and popFront out of static if hasSlicing!R

Now

    auto name = slidingSplitter("Nordlöw");

errors

range_ex.d(41,24): Error: incompatible types for ((this._index) += (stride(this._data, this._index))): 'ulong' and 'Result'
range_ex.d(88,12): Error: template instance range_ex.SlidingSplitter!string error instantiating
range_ex.d(131,32):        instantiated from here: slidingSplitter!string
October 03, 2014
>
> Is prefix ++ preferred in D because of some specific reason? I recall it, for some containers/iterators, gives smaller/faster codegen in C++?

I assume the reason is the same as in C++. As users can provide their own implementations of pre and postfix incrementing, the compiler can't assume that x++ always means "copy x then increment x and return the copy". It could have arbitrary side effects. Therefore for user defined types x++ can't be replaced with ++x by the compiler even if you're not using the return value. The compiler _can_ do this for primitive types, but as monarch_dodra says, if you get in the habit of using ++x unless you explicitly need x++ then you're never going to suffer and can sometimes get a speed win.
October 03, 2014
On Friday, 3 October 2014 at 20:28:24 UTC, Nordlöw wrote:
> On Friday, 3 October 2014 at 20:15:33 UTC, Nordlöw wrote:
>> Could you please take a look again at
>
> I made another update at
>
> https://github.com/nordlow/justd/blob/master/range_ex.d#L15
>
> I had forgotten to move front and popFront out of static if hasSlicing!R
>
> Now
>
>     auto name = slidingSplitter("Nordlöw");
>
> errors
>
> range_ex.d(41,24): Error: incompatible types for ((this._index) += (stride(this._data, this._index))): 'ulong' and 'Result'
> range_ex.d(88,12): Error: template instance range_ex.SlidingSplitter!string error instantiating
> range_ex.d(131,32):        instantiated from here: slidingSplitter!string

It must be resolving to std.range.stride. You want std.utf.stride.
October 03, 2014
On Friday, 3 October 2014 at 20:15:33 UTC, Nordlöw wrote:
> Note that I had to tweak empty() a bit. I don't know if I got right. Could you check?

Sounds about right, but I didn't really look.

> Further I can't get string support to work:
>
>     writefln("%(%s\n%)", slidingSplitter("Nordlöw"));
>
> errors as
>
> std.format.FormatException@/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/format.d(2591): Expected '%s' format specifier for type 'SlidingSplitter!string'
>
> and
>
>     foreach (e; name)
>     {
>         version(show) writeln(e);
>     }
>
> errors as
>
> range_ex.d(132,5): Error: invalid foreach aggregate name, define opApply(), range primitives, or use .tupleof
>
> which diagnostics I btw is responsible for :)
>
> Could you please check?

Sounds like your range isn't a range. Did you check that:
isInputRange!(SlidingSplitter!string)
?

As mentioned in the first reply, it's a very important check, as it's very easy to get wrong.

For example, if you forget to mark front and empty as properties, then you don't have the correct range interface I believe.

> On Friday, 3 October 2014 at 17:46:18 UTC, monarch_dodra wrote:
>>        writefln("%(%s\n%)", slidingSplitter("Nordlöw"));
>
> That's a really cool syntax, btw. I got to remember that.

Yup. Love it. Use it.
October 03, 2014
On Friday, 3 October 2014 at 21:17:54 UTC, monarch_dodra wrote:
> Sounds about right, but I didn't really look.

std.utf.stride solved all but one thing...namely that

https://github.com/nordlow/justd/blob/master/range_ex.d#L131

prints all but last

Tuple!(string, string)("", "Nordlöw")
Tuple!(string, string)("N", "ordlöw")
Tuple!(string, string)("No", "rdlöw")
Tuple!(string, string)("Nor", "dlöw")
Tuple!(string, string)("Nord", "löw")
Tuple!(string, string)("Nordl", "öw")
Tuple!(string, string)("Nordlö", "w")

I'm guessing empty() is the problem but changing

    _data.length == _index

to

    _data.length < _index

instead leads to infinite iteration

Tuple!(string, string)("", "Nordlöw")
Tuple!(string, string)("N", "ordlöw")
Tuple!(string, string)("No", "rdlöw")
Tuple!(string, string)("Nor", "dlöw")
Tuple!(string, string)("Nord", "löw")
Tuple!(string, string)("Nordl", "öw")
Tuple!(string, string)("Nordlö", "w")
Tuple!(string, string)("Nordlöw", "")
Tuple!(string, string)("Nordlöw", "")
Tuple!(string, string)("Nordlöw", "")
Tuple!(string, string)("Nordlöw", "")
Tuple!(string, string)("Nordlöw", "")
Tuple!(string, string)("Nordlöw", "")

...

Why?
October 03, 2014
On Friday, 3 October 2014 at 21:34:33 UTC, Nordlöw wrote:
> Why?

I cracked it :)

See: https://github.com/nordlow/justd/blob/master/range_ex.d#L36

Thanks for all your help!

Now I know a lot more about ranges.

Do you think anybody is interested in including this in Phobos?
1 2
Next ›   Last »