Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opensource C++ LINQ library with dot notation, orderBy and firstOrDefault?

I search for a VS2010 compatable C++ linq library with C# LINQ dot sintax. meaning something like: from(...).where(...).orderBy.firstOrDefault();

I googled and found this so answer LINQ libraries collection/mess:

  • Rx Extensions cpplinq has no orderBy
  • Boolinq has strange orderBy behaviour and no first out of the box
  • cppex (cppextensions) code I tested (similar to this) crashed vs2010 compiler (C1001)
  • linqxx has no orderBy

Others I found not using dot notation.. btw pfultz2/Linq seems to provide orderBy and first yet its SQL like LINQ sintax and Limitations make it something I am not looking for=(

So Is ther any opensource C++ LINQ library with dot notation, orderBy and firstOrDefault?

like image 596
myWallJSON Avatar asked Mar 16 '13 20:03

myWallJSON


1 Answers

Well, I will not give you the reply you want, but it will be a reply anyway :-)

LINQ is thought for C# mainly. I think your use case should be for translating C# code into C++, but I think that the effective way in C++ is to use Boost.Range.

Boost.Range reuses the c++ standard library in a way that is easy to do query on data:

  1. You can use adaptors for containers with left to right notation using operator |. They are evaluated lazily, just as in LINQ.
  2. You can perform operations such as std::min, std::max, std::all_of, std::any_of, std::none_of in the adapted ranges.

An example I wrote the other day is how to reverse words in a string. The solution was something like this:

using string_range = boost::iterator_range<std::string::const_iterator>;

struct submatch_to_string_range {
    using result_type = string_range;

    template <class T>
    string_range operator()(T const & s) const {
        return string_range(s.first, s.second);

    }
};

string sentence = "This is a sentence";

auto words_query = sentence |
                ba::tokenized(R"((\w+))") |
                ba::transformed(submatch_to_string_range{}) |
                ba::reversed;         


vector<string_range> words(words_query.begin(), words_query.end());

for (auto const & w : words) 
cout << words << endl;

I highly recommend you to base your queries upon this library, since this is going to be supported for a very long time and I think it will be the future. You can do same style of queries.

It would be nice if this library could be extended with things like | max and | to_vector to avoid naming the vector directly and copying, but I think that as a query language, right now, it's more than acceptable.

like image 178
Germán Diago Avatar answered Oct 19 '22 23:10

Germán Diago