Skip to content

mohitanand001/underscore_cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

underscore_cpp

Inspired by underscore.js

Table of contents:

┬ ┬┌┐┌┌┬┐┌─┐┬─┐┌─┐┌─┐┌─┐┬─┐┌─┐    ┌─┐┌─┐┌─┐
│ ││││ ││├┤ ├┬┘└─┐│  │ │├┬┘├┤     │  ├─┘├─┘
└─┘┘└┘─┴┘└─┘┴└─└─┘└─┘└─┘┴└─└─┘────└─┘┴  ┴  

Why underscore_cpp

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.

Implementation of

  • each
  • transform
  • filter_accept
  • filter_reject
  • find_if
  • find_if_not
  • every
  • reduce
  • count_by
  • contains
  • max
  • min
  • intersect
  • union
  • size

Setup and build

  1. Clone the project using git clone https://github.com/farziengineer/underscore_cpp
  2. To first configure the project build you will need cmake (3.8 or higher), from the project's root directory type cmake .
  3. 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

Usage

each

void display(int x)
{
   cout << x << " ";
}

std::vector<int> vec = {1, 2, 3};

_::each(vec, display);

Output: 1 2 3

transform

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

filter_accept

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

filter_reject

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

find_if

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

find_if_not

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)

every

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)

any

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)

reduce

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)

count_by

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)

contains

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)

max

std::vector<int> vec = {2, 4, 5}; 
std:: cout << _::max(vec) << std::endl;

Output: 5

min

std::vector<int> vec = {2, 4, 5}; 
std:: cout << _::max(vec) << std::endl;

Output: 2

size

std::vector<int> vec = {2, 4, 5};
std::cout << _::size(vec) << std::endl;

Output: 3

Contribution Guidelines

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.