std.v2020.algorithm etc[ WAS: Is run.d going to be expand for runtime and the phobos library?]

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Jun 28 15:07:21 UTC 2020


On Sun, Jun 28, 2020 at 02:00:42PM +0000, Stanislav Blinov via Digitalmars-d wrote:
[...]
> What makes an input range distinct is that, unlike other kinds, it can
> only be consumed once - there's no need for any other assumptions. The
> current range API does not enforce this in any way, hence the copying
> conundrum and Jonathan's "don't use original after you make a copy" or
> other similar statements that amount to "you should know what you're
> doing".

This is not a sufficient assumption, because there remains the question
of what state a range should be in after you iterate over it partially,
say, using a foreach loop:

	MyRange r = ...;
	foreach (e; r) {
		if (someCondition) break;
	}
	foreach (e; r) {
		// Do we get the remaining elements of r here, or do we
		// get all the elements from the beginning again?
	}

Even if we grant that r is a forward range, the semantics of the above
code is still underspecified.

And it's not just a matter of specifying that the semantics should be;
there's also the question of whether we should specify anything at all,
keeping in mind that the more specific the definition, the less room
there is for efficient implementation of ranges (i.e., some
optimizations may be excluded if there are too many semantic
requirements on ranges).


[...]
> Input ranges are treated as a subset of forward ranges. But they're
> not - they're distinct, non-intersecting sets.

OTOH, there is a great deal of advantage in using the subset of
semantics in which input ranges *are* subsets of forward ranges, because
this allows us to unify a lot of iteration logic.  Otherwise, like you
say below, every algorithm will need to be duplicated, once for input
ranges, once for forward ranges, which will instantly double the size of
Phobos algorithms.


[...]
> auto algo(Range)(Range range)
> if (isInputRange!Range)
> { /* ... */ }
> 
> auto algo(Range)(Range range)
> if (isForwardRange!Range)
> { /* ... */ }
[...]


T

-- 
If it tastes good, it's probably bad for you.


More information about the Digitalmars-d mailing list