pythonic++: A library for adding python-like syntactic sugar to C++11.
This header file tries to create some simple utility functions that are inspired by Python inbuilt functions such as len
, min
, etc. A brief summary of all the functionality in the package is below
The base iterable<T,R>
type is at the core of the provided functionality. This class is the base of a generic iterable that supports forward iteration only, and exopses an iterator that is either of ITERATOR_END
type, or ITERATOR_NORMAL
type. Note that we treat any two END
iterators as equal, while any other pair of iterators as non-equal. This is because in a generic iterable there is no way to ensure that two iterators are pointing to the exact same iteration point. Thus, the only type of iteration supported by any iterable
is of the type
for(auto it = itr.begin(); it != itr.end(); ++it) {...}
OR
for(auto v : itr) {...}
Any iterable supports the following methods:
filter(Function f)
: returns anotheriterable
that only contains elementsx
for whichf(x) == true
.transform(Function f)
: returns anotheriterable
with the valuesf(x)
for eachx
in theiterable
.enumerate()
: returns anotheriterable
that contains apair<size_t, T>(i,x)
wherei
is the iteration index for eachx
.max()
: returns the maximum valuex
seen in this iterable.max(Function f)
: returns the valuex
such thatf(x)
is maximum.min()
: returns the minimum valuex
seen in this iterable.min(Function f)
: returns the valuex
such thatf(x)
is minimum.len()
: returns the length of this iterable.foreach(Function f)
: returns the originaliterable
while performing the computationf(x)
for eachx
. Note that this does not a changediterable
. For changing, see the functiontransform
.to_vector()
: returns avector<T>
that contains each value from thisiterable
in sequence.(Pushable)
: casts the iterable to the typePushable
that supports apush_back
operation.
For STL containers that are not derived from the iterable
class but support iteration by begin()
, end()
, and ++
, the library defines convenience functions similar to the above. Specifically each iterable::member_function(...)
has a counterpart member_function(v, ...)
which can operate on any such STL container. Two more functions are defined on STL containers:
foreach(V &v, Function fun, IterFunction ifun)
: This is equivalent tofor(auto it = v.begin(); it != v.end(); it = iterfun(it)) { fun(*it); }
foreach(V &v, Function fun, V::iterator beginit, V::iterator endit, IterFunction ifun)
: This is equivalent tofor(auto it = beginit; it != endit; it = iterfun(it)) { fun(*it); }
Also, there is a function range()
which iterates over all integers in a given range. The variations are:
range(N)
: iterates through0, 1, ..., N-1
range(M,N)
: iterates throughM, M+1, ..., N-1
range(M,N,step)
: iterates throughM, M+step, M+2*step, ..., N-1
Some simple examples that illustrate the functionality of the library is in the tests/test.cpp file.