Skip to content

melaniew/underscore_cpp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

underscore_cpp

Inspired by underscore.js

Table of contents:

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

Implementation of

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

Setup

  1. Clone the project using git clone https://github.com/farziengineer/underscore_cpp
  2. To run the project you can individually compile the files(.cpp) and link them, or just run the bash file runn.sh using bash runn.sh (on Linux).

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, 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];

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];

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)

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

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.

About

underscore_cpp

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 99.0%
  • Shell 1.0%