Search code examples
c++boostfunctional-programminglazy-evaluationboost-hana

Taking n lines from a file which contains m lines, repeating the file (lazily) if necessary


I'm looking for a way to use a text file to generate as much text as I need by simply repeating the as few times as needed.

In a purely functional language, like Haskell, the solution seems trivial: here I've posted the code for the review, and it is pretty short, even though I'm pretty sure it can be improved.

But in C++, I have little clue of where to start from, except that I'm pretty sure Boost Hana offers a lot of the tools I need to design the solution.

Here's an example input file,

line 1
line 2
line 3

and, if I ask for 7 lines, here's what I'd like to put in a variable (for instance in a single std::string with embedded '\n's),

line 1
line 2
line 3
line 1
line 2
line 3
line 1

I guess the function can have a declaration like this:

std::string function(std::string s, int n);

Solution

  • Assuming file is some input stream, and you want to repeat the lines in file for n lines, you can use the range-v3 library like this:

    namespace rv = ranges::views;
    
    auto lines = ranges::getlines(file) 
               | ranges::to<std::vector<std::string>>;
      
    auto result = lines 
                | rv::cycle
                | rv::take(n) 
                | rv::join('\n') 
                | ranges::to<std::string>;
    

    Here's a demo.