Is there a LINQ library for C++?

C++LinqUnix

C++ Problem Overview


Are there any Platform agnostic (not CLI) movements to get LINQ going for C++ in some fashion?

I mean a great part of server frameworks around the world run on flavors of UNIX and having access to LINQ for C++ on UNIX would probably make lots of people happy!

C++ Solutions


Solution 1 - C++

Linq++ by Hong Jiang looks like a good start. Its syntax is much closer to Linq than CLinq's. Linq by pfultz2 looks interesting, as well, but it needs a C++11 compiler.

Solution 2 - C++

This is my solution of template C++ LINQ library.
Source code is here: Boolinq
There are a lot of tests on each feature.
I'm working on it right now.

Any comments?
May be advices?

UPDATE: project moved to https://github.com/k06a/boolinq and now have version 2.0 with only 700 lines of source code :)

Solution 3 - C++

Microsoft has just announced that they've built LINQ for C and C++. Not yet available, though.

Update 11/06/2012:

Microsoft Open Technologies, Inc. has now released and open-sourced (Apache License 2.0) a number of related libraries, including a LINQ implementation (Ix++), and it's new Reactive Extensions (Rx++) library.

Solution 4 - C++

http://cpplinq.codeplex.com/ is a very good implementation.
From the author:
The motivation for CppLinq is that both boolinq and Native-RX seems to be based around the operator"." to compose list functions. The problem is that the "." operator is that it can't be overloaded in C++ which makes it hard to extend these libraries with functions of my own design. To me this is important. CppLinq is based around operator>> which is overloadable thus CppLinq can be made extensible.

Solution 5 - C++

You may take a look at PSade.Oven, a strongly boostified library working on STL ranges and providing a lot LINQ like functions.

Solution 6 - C++

I have written a small library cppLinq that reimplements IEnumerable<> and its LINQ operators. It is just an experiment; for now it only works on Windows (coroutines are implemented with Win32 fibers), and only builds with the Dev Preview of VS11 (it makes an heavy usage of lambda expressions :-)).

It allows to write code like this:

auto source = IEnumerable<int>::Range(0, 10);

auto it = source->Where([](int val) { return ((val % 2) == 0); })
                ->Select<double>([](int val) -> double { return (val * val); }));

foreach<double>(it, [](double& val){
    printf("%.2f\n", val);
});

Solution 7 - C++

Actually if you just want to use Linq for list comprehension, you can use this Linq library. It requires C++11(it will work in MSVC 2010 though) and Boost. With the library you can write linq queries like this:

struct student_t
{
    std::string last_name;
    std::vector<int> scores;
};

std::vector<student_t> students = 
{
    {"Omelchenko", {97, 72, 81, 60}},
    {"O'Donnell", {75, 84, 91, 39}},
    {"Mortensen", {88, 94, 65, 85}},
    {"Garcia", {97, 89, 85, 82}},
    {"Beebe", {35, 72, 91, 70}} 
};

auto scores = LINQ(from(student, students) 
                   from(score, student.scores) 
                   where(score > 90) 
                   select(std::make_pair(student.last_name, score)));

for (auto x : scores)
{
    printf("%s score: %i\n", x.first.c_str(), x.second);
}

Which will output:

Omelchenko score: 97
O'Donnell score: 91
Mortensen score: 94
Garcia score: 97
Beebe score: 91

Solution 8 - C++

C++ 0x, or whatever it ends up being called, does have a new keyword called auto which allows for type-inference. And yes, there will be lambda's coming for C++. Also, a quick Google search revealed this, CLinq.

Solution 9 - C++

Here is another alternative that is simply a wrapper around boost and stl algorithms, and thus you get most of the performance benefits of those implementations.

It works like this:

std::vector<int> xs;
auto count = from(xs)
   .select([](int x){return x*x;})
   .where([](int x){return x > 16;})
   .count();
auto xs2 = from(xs)
   .select([](int x){return x*x;})
   .to<std::vector<int>>();

Note that some methods return a proxy for empty ranges, e.g.

std::vector<int> xs;
auto max = from(xs)
   .select([](int x){return x*x;})
   .where([](int x){return x > 16;})
   .max()
   .value_or(default_max_value);

Feedback is welcome.

Solution 10 - C++

Here is my implemention of c++-linq with c++11(in chinese):

http://www.cnblogs.com/cbscan/archive/2012/10/20/2732773.html

It support features like "deferred query","stack based"(use operator new as little as possible),"copy semantic"(so you can iterate a query multitime after backup it), and so on.

It also support dozens of function including "from, select, where, cast ,range, all, any ,cast, average ,contain, count ,first, last, head, tail ,groupBy ,takeUntil, skipUntil ,max, min ,reduce ,unique, sort, random ,intersect, _union".

I think my code is simple enough to understand and extend by anybody selves.

Solution 11 - C++

I don't think C++ has the compiler sugar to handle things such as lambda expressions, so no, that's not going to happen.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobert GouldView Question on Stackoverflow
Solution 1 - C++js.View Answer on Stackoverflow
Solution 2 - C++k06aView Answer on Stackoverflow
Solution 3 - C++lightw8View Answer on Stackoverflow
Solution 4 - C++yufanyufanView Answer on Stackoverflow
Solution 5 - C++zwvistaView Answer on Stackoverflow
Solution 6 - C++Paolo SeveriniView Answer on Stackoverflow
Solution 7 - C++Paul Fultz IIView Answer on Stackoverflow
Solution 8 - C++ChrisView Answer on Stackoverflow
Solution 9 - C++ronagView Answer on Stackoverflow
Solution 10 - C++ScanView Answer on Stackoverflow
Solution 11 - C++FlySwatView Answer on Stackoverflow