Project Description

LINQ for C++ is an attempt to bring LINQ-like list manipulation to C++11.

How to use it:

  1. Download the latest version of the source code from here (it's small): http://cpplinq.codeplex.com/downloads/get/616650
  2. Add the header file to your C++ project
  3. include the header in the source files were you want to use the query operators (which are defined in the cpplinq namespace)
  4. In VC++ you have to define NOMINMAX in order to avoid including the min/max macros. If you use NuGet, this is automatically defined.
Supported compilers
  • Windows
    • Visual Studio 2010 (Toolkit v100) (Note: unit tests do not compile with this version)
    • Visual Studio 2012 (Toolkit v110)
    • MinGW (g++ v4.7.0)
  • Linux
    • g++ v4.7.0
    • clang++ v3.1

NuGet Package for Visual Studio

A NuGet package for Visual Studio 2010 and 2012 is available here. You can directly install and update the package from Visual Studio, or run this command in a PowerShell window.
Install-Package cpplinq

When you use NuGet to automatically add the library, all the necessary settings (include folder, NOMINMAX pre-processor definition, etc.) are performed automatically.

Samples

The following example shows how to compute the sum of the even numbers from an array.
#include "cpplinq.hpp"

void computes_a_sum ()
{
    using namespace cpplinq;
    int ints[] = {3,1,4,1,5,9,2,6,5,4};

    auto result =    from_array (ints)
                  >> where ([](int i) {return i%2 ==0;})  // Keep only even numbers
                  >> sum ()                               // Sum remaining numbers
                  ;
}
The next sample shows how to identify the distinct elements of a sequence.
int ints[] = {3,1,4,1,5,9,2,6,5,4};
auto result =    from_array(ints)
              >> distinct()   // identify distinct elements
              >> to_vector()  // fold the result to a vector
              ;
Given the customer and customer_address types shown bellow and the arrays with customers and addresses in this listing
struct customer
{
    std::size_t     id          ;
    std::string     first_name  ;
    std::string     last_name   ;
};

struct customer_address
{
    std::size_t     id          ;
    std::size_t     customer_id ;
    std::string     country     ;
};

customer customers[] = 
{
    customer (1 , "Bill"    , "Gates"   ),
    customer (2 , "Steve"   , "Jobs"    ),
    customer (3 , "Richard" , "Stallman"),
    customer (4 , "Linus"   , "Torvalds"),
};

customer_address customer_addresses[] =
{
    customer_address (1, 1, "USA"       ),
    customer_address (2, 4, "Finland"   ),
    customer_address (3, 4, "USA"       ),
};
we can join them with the following piece of code:
auto result = 
       from_array(customers)                                                                   
    >> join (
            from_array(customer_addresses),
            [](customer const & c) {return c.id;},
            [](customer_address const & ca) {return ca.customer_id;},
            [](customer const & c, customer_address const & ca) {return std::make_pair (c, ca);},
       )
    >> to_vector();

About

LINQ originally referred to the SQL-like syntax in C# and VB but it has over time changed its meaning to mean the way you manipulate lists using the higher-order functions provided by System.Linq. Working with lists using higher-order functions have been available for functional and SmallTalk developers since the 70s but has in recent been popularized in main-stream languages such as C#, VB, JavaScript, Python, Ruby and so on. CppLinq is a library that tries to bring this style of programming to C++.

There are already several attempts at writing a LINQ library for C++, for instance:
  1. boolinq http://code.google.com/p/boolinq/
  2. Native-RX http://channel9.msdn.com/Shows/C9-GoingNative/GoingNative-9-LINQ-for-C-Native-Rx-RxC-Meet-Aaron-Lahman

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.

Documentation

See documentation in order to get started and for more advanced topics such as creating your own range operators or range aggregators.

Last edited Wed at 12:16 PM by MariusBancila, version 22