GithubHelp home page GithubHelp logo

functools's Introduction

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.

functools's People

Contributors

maganer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.