streams to ranges adapters

Diego Martinelli martinelli.diego at gmail.com
Wed Sep 1 03:19:41 PDT 2010


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


More information about the Digitalmars-d-learn mailing list