Not Another Range Library - Overload at ACCU: an article introducing the library - Range and Elevation
#Get started
Narl is a header-only library for C++. You'll need gcc 4.8 or better, or MS Visual Studio 2013. Not all the features are available in Visual Studio. The code also compiles with Clang 3.2, with some warnings.
Add the src/narl
folder to you search path and include <narl.h>
.
The library is based around the idea of Ranges, but for the most part, these aren't directly used. Instead, you compose expressions from Narl, and let the compiler figure out what the result is.
There are lots of expressions which you can compose together, using the | operator. Here is a simple filtering expression:
For example:
#include <narl.h>
#include <iostream>
#include <string>
#include <vector>
using namespace narl;
int main( int argc, char *const argv[] )
{
std::vector< std::string > src { argv, argv + argc };
auto r = from( src ) | where( []( const item & i ) { return i.size() > 0; } );
for( auto const & arg : r )
{
std::cout << arg;
}
}
You can use initializer lists to initialize a range too: from( { 1, 2, 3 } )
.
Expressions can be arbitrarily complex, and are lazily evaluated left-to-right:
auto r = from( src )
| where( []( const item & i ) { return i.size() > 0; } )
| select( []( const item & i ) { return i.name(); } );
Lastly, you can "re-hydrate" a range back into a standard container with to
:
auto r = from( { 1, 2, 3 } )
| select( []( int i ) { return std::to_string( i ); } )
| to< std::list >();
// or to< std::vector >() for example
The full list of implemented expressions is:
from to make_range fill_range range
aggregate all any concat count cycle distinct except groupby join* intersect_with merge_with reverse select selectmany sequenceequal skip skip_while sorted take take_while union_with where zipwith
(*join does not work in VS2013)
For some example code, see narl_stl.cpp which has some narl expressions showing implementations of C++ standard algorithms.
Narl uses Catch which is referenced as an external git project in externals/catch
You'll need to initialize and update the submodule reference if you fork narl.
Test sources are in the src/tests/testnarl
folder. There is a makefile for g++ and a .sln file for Visual Studio in src
.