www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - streams to ranges adapters

reply Diego Martinelli <martinelli.diego gmail.com> writes:
Hi all!
I'm digging the documentation of phobos but I am still learning D and I can't
help
myself.
I feel like there's a lack of adapters between streams and ranges (something
like
istream_iterator/ostream_iterator in STL).

Just a bit of C++ to make my point:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;  // just for convenience

int factorial(const int& n) {
  int result = 1;
  for (int i = 1; i <= n; ++i)
    result *= i;
  return result;
}

int main() {
  vector<int> v;

  copy(istream_iterator<int>(cin),
       istream_iterator<int>(),
       back_inserter(v));

  transform(v.begin(), v.end(),
            ostream_iterator<int>(cout, "\n"),
            factorial);

  return 0;
}

How'd you write an equivalent program in idiomatic D? I mean, I'd like an higher
level approach to foreach/readf. Am I missing something?

Thanks,
Diego
Sep 01 2010
next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Wednesday 01 September 2010 03:19:41 Diego Martinelli wrote:
 Hi all!
 I'm digging the documentation of phobos but I am still learning D and I
 can't help myself.
 I feel like there's a lack of adapters between streams and ranges
 (something like istream_iterator/ostream_iterator in STL).

A properly written stream type _is_ a range with no adaptors necessary. However, std.stream predates much of the range stuff in Phobos, so its implementation does not support ranges. The intention is to replace it with a streaming module that _does_ properly support ranges. However, it's on the TODO list and hasn't been done yet. IIRC, there's at least one implementation which has been discussed on the Phobos list which may end up as the replacement for std.stream, but it hasn't happened yet, so Phobos does not yet support using ranges with streams. - Jonathan M Davis
Sep 01 2010
prev sibling next sibling parent Peter Neubauer <peterneubauer2 gmail.com> writes:
On 09/01/2010 12:19 PM, Diego Martinelli wrote:
 Hi all!
 I'm digging the documentation of phobos but I am still learning D and 

 myself.
 I feel like there's a lack of adapters between streams and ranges 

 istream_iterator/ostream_iterator in STL).

I've encountered a similar problem yesterday. It left me digging through the Phobos docs for an hour looking for an existing solution :-) Let's say I have a function (or any D expression,) and I want to turn consecutive calls to this function into range form. As an example, let's generate 10 random numbers between 30 and 50. My first attempt was something like this: import std.range; import std.random; void main () { writeln(take(recurrence!("uniform(30, 50)")(), 10)); } However, this doesn't work because recurrence refuses to take 0 arguments. Your post prompted me to steal some boilerplate code from the Phobos source and write this: struct Rangeof(T) { T delegate() _dg; T _buffer; bool _called = false; this(T delegate() dg) { _dg = dg; } property bool empty() { return false; } void popFront() { if (!_called) { _buffer = _dg(); } _called = false; } property ref T front() { if (!_called) { _buffer = _dg(); _called = true; } return _buffer; } } auto rangeof(F)(F func) if (isCallable!F) { return Rangeof!(ReturnType!(F))(func); } Rangeof!(T) rangeof(T)(lazy T exp) if (!isCallable!T) { T callexp (){ return exp; } return rangeof(&callexp); } Now you can turn any expression or function into a proper input range, compatible with std.range stuff. Example: writeln(take(rangeof(uniform(30, 50)), 10)); At the risk of overshooting my goal, here's an additional overload for std.range.take which can now also "take" from a function: auto my_take(F)(F input, size_t n) if (!isInputRange!F && isCallable!F) { return Take!(Rangeof!(ReturnType!(F)))(rangeof(input), n); } Which means one less step in usage: writeln(my_take({ return uniform(30, 50); }, 10)); Oh, and I didn't test my code at all, apart from those examples. Feel free to use it if you like. -Peter
Sep 01 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--001636c5a4f7d49166048f385453
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Sep 1, 2010 at 20:54, Peter Neubauer <peterneubauer2 gmail.com>wrote:

 I've encountered a similar problem yesterday. It left me digging through
 the Phobos docs for an hour looking for an existing solution :-) Let's say I
 have a function (or any D expression,) and I want to turn consecutive calls
 to this function into range form. As an example, let's generate 10 random
 numbers between 30 and 50. My first attempt was something like this:

 Oh, and I didn't test my code at all, apart from those examples. Feel free
 to use it if you like.

It's a good idea and a common need: to transform a generator into a range. It could be adapted to work with callable structs and classes, too, if you need it to hold state. Philippe --001636c5a4f7d49166048f385453 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Wed, Sep 1, 2010 at 20:54, Peter Neub= auer <span dir=3D"ltr">&lt;<a href=3D"mailto:peterneubauer2 gmail.com">pete= rneubauer2 gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_qu= ote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 20= 4, 204); padding-left: 1ex;"> <div class=3D"im"><br></div> I&#39;ve encountered a similar problem yesterday. It left me digging throug= h the Phobos docs for an hour looking for an existing solution :-) Let&#39;= s say I have a function (or any D expression,) and I want to turn consecuti= ve calls to this function into range form. As an example, let&#39;s generat= e 10 random numbers between 30 and 50. My first attempt was something like = this:<br> </blockquote><div>(snip) <br></div><blockquote class=3D"gmail_quote" style= =3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); p= adding-left: 1ex;">Oh, and I didn&#39;t test my code at all, apart from tho= se examples. Feel free to use it if you like.<font color=3D"#888888"></font=
</blockquote>

nto a range. It could be adapted to work with callable structs and classes,= too, if you need it to hold state.<br><br><br>Philippe<br>=A0<br></div></d= iv> --001636c5a4f7d49166048f385453--
Sep 01 2010