Inspired by underscore.js
┬ ┬┌┐┌┌┬┐┌─┐┬─┐┌─┐┌─┐┌─┐┬─┐┌─┐ ┌─┐┌─┐┌─┐ │ ││││ ││├┤ ├┬┘└─┐│ │ │├┬┘├┤ │ ├─┘├─┘ └─┘┘└┘─┴┘└─┘┴└─└─┘└─┘└─┘┴└─└─┘────└─┘┴ ┴
underscore_cpp provides a range of generic functions for operations involving containers in c++. The library aims to help writing cleaner code by providing functional programming helpers.
- each
- transform
- filter_accept
- filter_reject
- find_if
- find_if_not
- every
- reduce
- count_by
- contains
- max
- min
- intersect
- union
- size
- Clone the project using
git clone https://github.com/farziengineer/underscore_cpp
- To first configure the project build you will need cmake (3.8 or higher), from the project's root directory type
cmake .
- To build type
make -j 4
The build will create a shared library:
libunderscore_shared.so
(linux)libunderscore_shared.dylib
(macos)libunderscore_shared.dll
(windows)
And an executable for the tests underscore_tests
void display(int x)
{
cout << x << " ";
}
std::vector<int> vec = {1, 2, 3};
_::each(vec, display);
Output: 1 2 3
int increment_by_one(int x)
{
return x + 1;
}
void display(int x)
{
cout << x << " ";
}
std::vector<int> vec = {1, 2, 3};
_::transform(vec.begin(), vec.end(), increment_by_one);
_::each(vec, display);
Output: 2 3 4
int is_odd(int x)
{
return x % 2 == 1;
}
void display(int x)
{
cout << x << " ";
}
std::vector<int> vec = {1, 2, 3};
std::vector<int> res = _::filter_accept(vec, is_odd);
_::each(res, display);
Output: 1 3
int is_odd(int x)
{
return x % 2 == 1;
}
void display(int x)
{
cout << x << " ";
}
std::vector<int> vec = {1, 2, 3};
std::vector<int> res = _::filter_reject(vec, is_odd);
_::each(res, display);
Output: 2
int is_odd(int x)
{
return x % 2 == 1;
}
void display(int x)
{
cout << x << " ";
}
std::vector<int> vec = {2, 4, 5};
int index = _::find_if(vec, is_odd) - vec.begin();
std::cout << vec[index] << std::endl;
Output: 5
int is_odd(int x)
{
return x % 2 == 1;
}
void display(int x)
{
cout << x << " ";
}
std::vector<int> vec = {1, 2, 4, 5};
int index = _::find_if_not(vec, is_odd) - vec.begin();
std::cout << vec[index] << std::endl;
Output: 2 (returns first index where the container value returns a false over predicate)
int is_odd(int x)
{
return x % 2 == 1;
}
std::vector<int> vec = {3, 7, 5};
std:: cout << _::every(vec, is_odd) << std::endl;
Output: true (returns true if every container element return true over predicate)
int is_odd(int x)
{
return x % 2 == 1;
}
std::vector<int> vec = {2, 4, 5};
std:: cout << _::any(vec, is_odd) << std::endl;
Output: true (returns true if any container element return true over predicate)
int multiply(int x, int y)
{
return x * y;
}
std::vector<int> vec = {1, 2, 3, 4, 5};
std::cout << _::reduce(vec, multiply, 1) << std::endl;
Output: 120 (applies a function of two arguments cumulatively to the items of an iterable, from left to right, so as to reduce the sequence to a single value)
int is_odd(int x)
{
return x % 2 == 1;
}
std::vector<int> vec = {2, 4, 5};
std:: cout << _::count_by(vec, is_odd) << std::endl;
Output: 2 (counts occurrences where the container returns true over predicate. Here the predicate is is_odd)
std::vector<int> vec = {2, 4, 5};
std:: cout << _::contains(vec, 9) << std::endl;
Output: false (returns true if any container element return true over predicate)
std::vector<int> vec = {2, 4, 5};
std:: cout << _::max(vec) << std::endl;
Output: 5
std::vector<int> vec = {2, 4, 5};
std:: cout << _::max(vec) << std::endl;
Output: 2
std::vector<int> vec = {2, 4, 5};
std::cout << _::size(vec) << std::endl;
Output: 3
For contributing fork the repository make the appropriate changes and create a pull request. Before starting to work on an issue, please ask it to be assigned to yourself, since we do not want more than one person to work on the same issue.