Inspired by underscore.js
┬ ┬┌┐┌┌┬┐┌─┐┬─┐┌─┐┌─┐┌─┐┬─┐┌─┐ ┌─┐┌─┐┌─┐ │ ││││ ││├┤ ├┬┘└─┐│ │ │├┬┘├┤ │ ├─┘├─┘ └─┘┘└┘─┴┘└─┘┴└─└─┘└─┘└─┘┴└─└─┘────└─┘┴ ┴
- each
- transform
- filter_accept
- filter_reject
- find_if
- find_if_not
- every
- count_by
- contains
- max
- min
- intersect
- union
- size
- Clone the project using
git clone https://github.com/farziengineer/underscore_cpp
- To run the project you can individually compile the files(.cpp) and link them, or just run the bash file
runn.sh
usingbash runn.sh (on Linux)
.
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, 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];
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];
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 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
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.