Skip to content

MAGANER/Functools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

Functools

pretty simple and basic functional library for C++

What it can do?

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.

How to use

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

Why did i write this library?

Well, mostly because i don't like <algorithm> from c++ std.

About

pretty simple and basic library for C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages