www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - More love for output ranges

reply Artem Tarasov <lomereiter gmail.com> writes:
We have great support for input ranges and thus for the pull-based model of
programming, but the standard library offers almost nothing for the
push-based model :(  It utterly lacks tools for composing output ranges.

What I'd like to see working is this:

import std.stdio, std.algorithm, std.range;

void main() {
    auto printer = fork!(n => n % 2)(sink!(n => writeln("odd: ", n)),
                                                  sink!(n => writeln("even:
", n)))
        .filter!(n => n != 16)
        .map!(n => n * n);

    copy(iota(10), printer);
}

http://dpaste.dzfl.pl/612a8ad6
Oct 12 2013
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
12-Oct-2013 22:00, Artem Tarasov пишет:
 We have great support for input ranges and thus for the pull-based model
 of programming, but the standard library offers almost nothing for the
 push-based model :(  It utterly lacks tools for composing output ranges.

 What I'd like to see working is this:

 import std.stdio, std.algorithm, std.range;

 void main() {
      auto printer = fork!(n => n % 2)(sink!(n => writeln("odd: ", n)),
                                                    sink!(n =>
 writeln("even: ", n)))
          .filter!(n => n != 16)
          .map!(n => n * n);

      copy(iota(10), printer);
 }

 http://dpaste.dzfl.pl/612a8ad6
Exactly, I even recall coming up with the same ideas on this NG. Except that I find that non-predicative fork is interesting too - just forward the same stuff to N sinks. For example, calculate a hash of a message as it's being sent to the network. Moreover a lot of algorithms can come with *To/*Into suffix and forward stuff into an output range. IIRC I was behind that idea some time ago but got distracted with time, some things made it through though: (+ new API for std.regex has similar constructs with *Into suffix) IMO you are more then welcome to champion additions in this direction. -- Dmitry Olshansky
Oct 12 2013