pretty simple and basic functional library for C++
First of all you should know this library is pretty small.
It contains 4 functions only: map, filter, slice and reduce.
As you can guess, it was inspired by python.
i wanted to create pure function to work with basic data structures from c++ STL: vector
, list
and map
.
You need to include Functools.hpp
and optionally use namespace Functools
.
Also to uses functions for std::map
, you need to define MAP_FUNCTOOLS
before including library.
And you should use namespace Functools::mapFP
.
#define MAP_FUNCTOOLS
#include"Functools.hpp"
Because to work with map
objects, it does
#include<map>
1.map
vector<int> test = {1,2,3,4,5,6,7,8,9,10};
function<int(int)> add = [](int elem){return elem+1;};
auto result = Functools::map(test,add);
You will get copy of test, but add lambda is apllied to each element of test vector.
2.filter
vector<int> test = {1,2,3,4,5,6,7,8,9,10};
function<bool(int)> pred = [](int elem){return elem>5;};
auto result = Functools::filter(test,pred);
You will get vector of numbers higher then 5.
3.reduce
vector<int> test = {1,2,3,4,5,6,7,8,9,10};
function<int(int,int)> add = [](int a, int b){return a+b;};
auto result = Functools::reduce(test,add);
You will get 55. Just abstract accumulator.
4.slice
I don't like std::string::substr and there is no common way to get slice of linear structures.
So i added this function. It returns slice: [begin, end] โ S.
vector<int> test = { 1,2,3,4,5,6,7,8,9,10 };
auto new_slice = Functools::slice(test, 5, 10);
I added new simple function make_lambda
It takes reference to function and returns std::function object.
function<bool(int)> check = make_lambda(isspace);
Well, mostly because i don't like <algorithm>
from c++ std.