More tricky range semantics

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Jan 15 22:38:19 PST 2015


On Thu, Jan 15, 2015 at 05:52:45PM -0800, Andrei Alexandrescu via Digitalmars-d wrote:
> On 1/15/15 4:58 PM, Joseph Rushton Wakeling via Digitalmars-d wrote:
> >On 16/01/15 00:24, Andrei Alexandrescu via Digitalmars-d wrote:
> >>That's right. To simplify the problem space we might decree that
> >>forward (or better) ranges with reference semantics are not allowed.
> >>-- Andrei
> >
> >That would seem to place significant restrictions on the ability to
> >define effective random number generators and related functionality
> >... ?
> 
> Agreed. Pseudo-random generators are an interesting kind of range
> because they are forward yet do not iterate a "real" container. --
> Andrei

I've been wondering about that. Must ranges have an "underlying"
container? Or are they allowed to be more abstract entities that
basically function as generators, producing data on demand? (This
distinction is also somewhat related to transient ranges, in that the
difference between providing a "view" of some static underlying data vs.
computing something on-the-fly in a buffer that gets reused, could serve
as a deciding factor on how to deal with transient ranges.)

My feeling is that allowing the latter is much more powerful, and more
encompassing. Constructs like iota() and recurrence() belong to the
latter category, for example, and they do provide forward range like
functionality -- or they *could*, if they don't already, as it would be
trivial to implement. Allowing generating functions to be ranges also
allows one to plug in programmatic data generators as data sources in
UFCS chains, without needing to deal with an artificial distinction
between "view of underlying data" ranges and "generated-on-the-fly"
ranges.

In this sense, an RNG could be thought of as a complex variant of
recurrence(), except with a far more complicated generating expression
than what one would normally use with recurrence(). If recurrence()
qualifies as a range (and a forward range, no less), then an RNG ought
to qualify too.


T

-- 
Those who don't understand Unix are condemned to reinvent it, poorly.


More information about the Digitalmars-d mailing list