Overhauling the notion of output range
dsimcha
dsimcha at yahoo.com
Sun Jul 11 18:34:00 PDT 2010
== Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
> The notion of output range has been a tad vague in the past; up until
> now a range that wanted to qualify as an output range had to define a
> method called put.
> That definition is awkward though. For example, the std.algorithm.copy()
> primitive should work for an output range, but also for some other
> ranges that allow assignment to front.
> In related news, there's been this burning desire regarding a
> lightweight output range defined as simply a delegate that accepts
> const(char)[]. That range is an output range all right, and the way you
> output to it is by simply calling the delegate!
> All of the three mechanisms above are desirable for certain types. So
> imagine this dialog (paraphrased from
> http://www.youtube.com/watch?v=MrTsuvykUZk):
> Guy 1: We need a good output range interface, and we have three good
> candidates. We should make every one an output range.
> Guy 2: What do you mean, every one?
> Guy 1: E-V-E-R-Y O-N-E!!!!
> So, I defined isOutputRange!R to yield true if at least one of these
> conditions above is met: put() definition, input range with assignable
> front, delegate.
> Please refer to this code and this doc:
> http://www.dsource.org/projects/phobos/browser/trunk/phobos/std/range.d#L227
> http://erdani.com/d/phobos/std_range.html
> This confers a ton of flexibility to programmers who need to define
> output ranges. I've also reworked std.format to use put(r, e) so now it
> works with all output ranges seamlessly.
> Any thoughts would be appreciated. Thanks!
> Andrei
== Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
> The notion of output range has been a tad vague in the past; up until
> now a range that wanted to qualify as an output range had to define a
> method called put.
> That definition is awkward though. For example, the std.algorithm.copy()
> primitive should work for an output range, but also for some other
> ranges that allow assignment to front.
> In related news, there's been this burning desire regarding a
> lightweight output range defined as simply a delegate that accepts
> const(char)[]. That range is an output range all right, and the way you
> output to it is by simply calling the delegate!
> All of the three mechanisms above are desirable for certain types. So
> imagine this dialog (paraphrased from
> http://www.youtube.com/watch?v=MrTsuvykUZk):
> Guy 1: We need a good output range interface, and we have three good
> candidates. We should make every one an output range.
> Guy 2: What do you mean, every one?
> Guy 1: E-V-E-R-Y O-N-E!!!!
> So, I defined isOutputRange!R to yield true if at least one of these
> conditions above is met: put() definition, input range with assignable
> front, delegate.
> Please refer to this code and this doc:
> http://www.dsource.org/projects/phobos/browser/trunk/phobos/std/range.d#L227
> http://erdani.com/d/phobos/std_range.html
> This confers a ton of flexibility to programmers who need to define
> output ranges. I've also reworked std.format to use put(r, e) so now it
> works with all output ranges seamlessly.
> Any thoughts would be appreciated. Thanks!
> Andrei
Nice. I had quietly wondered why input ranges with assignable front aren't also
output ranges, but never gotten around to asking. Two comments:
1. What happens if you run out of space in the input range variety? I guess it
throws?
2. Would there be a "standard" way of signaling how much stuff was written to the
input range variety? I guess that since functions that output their results via
output ranges would usually return void, they could return an integer instead
indicating how much stuff was written.
3. While we're on the subject of improving output ranges, I was just thinking
before I read your post that it would be nice to have a Tee type in std.range,
since outputting to exactly one output range is kind of inflexible. Such a type
would itself be an output range. It would take in N output ranges where N > 1 as
instantiation parameters, and pass any input received to all of the underlying
ranges.
The use case I thought of for this is when I'm generating tons of data through a
monte carlo simulation and don't want to store it all in memory. Both Summary in
dstats and Histogram in dflplot can be used as output ranges. I'd love to write a
function that outputs results to an output range and use Tee to get both summary
statistics and a histogram.
More information about the Digitalmars-d
mailing list