0

Since the ranges-library is up for inclusion into the standard I was toying around with it for a while and I have some problems with very basic concepts.

The toy example I am struggling with is this:

#include <iostream>
#include <range/v3/all.hpp>
using namespace ranges;


int main (int argc, char const* argv[]) {
    auto v = view::iota(1, 10);
    std::default_random_engine gen;
    auto t = action::push_back(view::iota(0, 10)) | action::shuffle(gen);
    std::cout << t << "\n";
}

The fact that it is not printable kind of suggests to me that t is not really a proper range. Unfortunately the documentation (where there is any) really abuses auto, so I do not know what is going on. What do I have to do, why do I have to do it (and in case anyone does know: how is the result type going to be documented (imo proxy objects and auto don't mix well - see the Eigen library as an example)).

My second question is related to the cargo-culted action::push_back. If I omit it I get a concept error telling me that the iota-generated view needs to be mutable. Obviously a view is by design immutable, so I get the problem here, but is "pushing nothing" really the preferred way to lift things into a mutable/stateful object or are there alternatives?

Best, Richard

metalfox
  • 6,301
  • 1
  • 21
  • 43
Richard Vock
  • 1,286
  • 10
  • 23
  • I do not use the ranges library, but iam interested in it as well :) could you input the compiler diagnostic? AFAIK the iterators are lazily created. so it could be that you can not shuffle view::iota, since the data does not exist like in a vector. have you tried to generate an vector from view::iota and then shuffle that one? – jonas_toth Mar 29 '17 at 10:46
  • the push_back is doing exactly that. I was just wondering if that is the canonical way to do it. The idea behind the ranges library is what is usually referred to as streams / stream fusion. You work on lazy containers until at some point (hopefully the end) there is a manifestation step (actually executing and writing). Manifestation is usually done using either some fold or some copy. ::push_back seems to be a (subjectively) weird choice for the latter. Hence my second question. Ultimately I was interpreting actions wrong, which is why Caleth's answer really helped. – Richard Vock Mar 29 '17 at 13:25
  • 1
    Actions operate on containers, not views. You need to make your `iota_view` concrete - e.g. with `to_vector` - before passing it to `shuffle`. [Try `auto container = view::iota(1, 10) | to_vector | action::shuffle(gen);`](https://wandbox.org/permlink/mQBa6v6szm5XnZ7j). – Casey Mar 30 '17 at 15:37

1 Answers1

3

Your t instance is an action, not a range. That's the main reason why it isn't <<able.

You can see this by applying it to multiple containers, e.g.

#include <iostream>
#include <experimental/iterator>
#include <range/v3/all.hpp>
using namespace ranges;


int main (int argc, char const* argv[]) {
    std::default_random_engine gen;
    auto t = action::push_back(view::iota(0, 10)) | action::shuffle(gen);
    auto v1 = std::vector<int>{} | t;
    auto v2 = std::vector<int>{} | t;
    auto v3 = std::vector<int>{} | t;
    auto v4 = std::vector<int>{} | t;
    std::copy(v1.begin(), v1.end(), std::experimental::make_ostream_joiner(std::cout, ", "));
    std::cout << "\n";
    std::copy(v2.begin(), v2.end(), std::experimental::make_ostream_joiner(std::cout, ", "));
    std::cout << "\n";
    std::copy(v3.begin(), v3.end(), std::experimental::make_ostream_joiner(std::cout, ", "));
    std::cout << "\n";
    std::copy(v4.begin(), v4.end(), std::experimental::make_ostream_joiner(std::cout, ", "));
    std::cout << "\n";
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • I think you're missing `{}` after `std::vector` – Guillaume Racicot Mar 29 '17 at 13:12
  • Ah! I did interpret the push_back action to be a view<->action conversion, but when seeing it as an action modifying an empty container this actually answers both of my questions. The problem was a matter of (interpretative) perspective on actions, not types. – Richard Vock Mar 29 '17 at 13:21