[Boost-users] C# LINQ is cool. What about boost.qlang?

297 views
Skip to first unread message

Germán Diago

unread,
Oct 13, 2009, 10:43:49 AM10/13/09
to boost...@lists.boost.org
Hello. I've been reading a bit about C#'s linq and it's very cool in my opinion.
I would like to see something similar in c++, and boost is the ideal
place I think.
As I would like to see something similar, I started a prototype adapted to c++.

For now I've been playing with the basics:

-from, where and select operators.
-it just can be used (for now) with from, where and select, all in a
row, but this will change.
-it should work with any range: containers, boost.range, etc.
-it's just a c++0x version. It works great with lambdas (gcc svn has lambdas).
-Can use any boost.fusion sequence as the object type to query.
(Although the plan is to be able to give data providers to
use to query any type of source.

The prototype works in the following way. For example (C++0x) :

vector<int> numbers = {1, 2, 3, 4, 8, 5, 9 , 24, 19, 15, 12 }

auto query = from(numbers).where([](int i) { return i < 15 && i > 10}).
select(fields::full_object);

This will construct an object with the query (without evaluating it).
The original Range CANNOT be modified.
If you want to modify , copy the result to a new container (in the
future call query.to_vector(), query.to_list() to return a list or
vector of objects.

This will evaluate everything:

for (auto it = query.begin(); it != query.end(); ++it)
cout << *it << endl;

You can traverse the range as many times as you like, and
If you have, for example:

struct Doctor {
string name_;
string surname_;
string specializedin_;
};

BOOST_FUSION_ADAPT_STRUCT(Doctor...)

you can do, for example:

vector<Doctor> doctors = {....};

auto query = from(doctors).where([](const Doctor & d) { return
d.surname_ == "Smith"; }).
select(fields::_1);

The plan to improve readability is to use fusion associative sequence to do:

auto query = from(doctors).where([](const Doctor & d) { return
d.surname_ == "Smith"; }).
select(Doctorfields::name, Doctorfields::specializedin);

The query will return a tuple<string, string> in this case (the one
before this would return tuple<string>.
Queries with multiple fields don't work for now.

A first implementation with the ability to select the full object or a
field number (not several, but planned) is attached. Test it if you
want
and tell me what you think.
The plan would be the same as the .NET platform: to be able to query
XML, text files, objects, databases... giving
extension points to add data providers.
Another thing that doesn't work right now is to nest a query inside
another query, but I'll work on it.

Feedback is welcome.

qlangtest.cpp
qlang.hpp

joel

unread,
Oct 13, 2009, 11:48:26 AM10/13/09
to boost...@lists.boost.org
Germán Diago wrote:
> Hello. I've been reading a bit about C#'s linq and it's very cool in my opinion.
> I would like to see something similar in c++, and boost is the ideal
> place I think.
> As I would like to see something similar, I started a prototype adapted to c++.
>
> For now I've been playing with the basics:
>
> -from, where and select operators.
> -it just can be used (for now) with from, where and select, all in a
> row, but this will change.
> -it should work with any range: containers, boost.range, etc.
> -it's just a c++0x version. It works great with lambdas (gcc svn has lambdas).
> -Can use any boost.fusion sequence as the object type to query.
> (Although the plan is to be able to give data providers to
> use to query any type of source.
>
> The prototype works in the following way. For example (C++0x) :
>
> vector<int> numbers = {1, 2, 3, 4, 8, 5, 9 , 24, 19, 15, 12 }
>
> auto query = from(numbers).where([](int i) { return i < 15 && i > 10}).
> select(fields::full_object);
>
I've proposed somethign very similar at boost'con 09 for a higher level
query library.
I'm all for it

--
___________________________________________
Joel Falcou - Assistant Professor
PARALL Team - LRI - Universite Paris Sud XI
Tel : (+33)1 69 15 66 35


_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Germán Diago

unread,
Oct 13, 2009, 11:55:10 AM10/13/09
to boost...@lists.boost.org
> I've proposed somethign very similar at boost'con 09 for a higher level
> query library.
> I'm all for it


When I have something that works reasonably well, maybe I could upload
it elsewhere.

Reply all
Reply to author
Forward
0 new messages