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 anotheriterablethat only contains elementsxfor whichf(x) == true.transform(Function f): returns anotheriterablewith the valuesf(x)for eachxin theiterable.enumerate(): returns anotheriterablethat contains apair<size_t, T>(i,x)whereiis the iteration index for eachx.max(): returns the maximum valuexseen in this iterable.max(Function f): returns the valuexsuch thatf(x)is maximum.min(): returns the minimum valuexseen in this iterable.min(Function f): returns the valuexsuch thatf(x)is minimum.len(): returns the length of this iterable.foreach(Function f): returns the originaliterablewhile 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 thisiterablein sequence.(Pushable): casts the iterable to the typePushablethat supports apush_backoperation.
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-1range(M,N): iterates throughM, M+1, ..., N-1range(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.