You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* * Modifies the sequence container with the results of calling a provided * function on every element in this container. * * @returns {container<T>}*/
#include<vector>
#include<iostream>
#include"functional++.hpp"intmain () {
std::vector<int> vector = {0, 1, 2, 3, 4};
functional::map(vector, [](constint value) {
return value * 2;
});
for (auto value: vector) {
std::cout << value << std::endl; // 0, 2, 4, 6, 8
}
return0;
}
functional::filte(container, function);
/* * Filters the sequence container with all elements that pass the test implemented by the provided function. * * @returns {container<T>}*/
#include<vector>
#include<iostream>
#include"functional++.hpp"intmain () {
std::vector<int> vector = {0, 1, 2, 3, 4};
functional::filter(vector, [](constint value) {
return item % 2 == 0;
});
for (auto value: vector)
std::cout << value << std::endl; // 1, 3return0;
}
/* * Apply a function against an accumulator and each value of * the sequence container (from left-to-right) as to reduce it to a single value. * * @returns {container<T>::value_type}*/
#include<vector>
#include<string>
#include<iostream>
#include"functional++.hpp"intmain () {
// Example 1:
std::vector<int> vector = {0, 1, 2, 3, 4};
int result = functional::reduce(vector, [](constint current, constint prev) -> int {
return current + next;
});
std::cout << result << std::endl; // 10// Example 2:
std::vector<int> vector = {0, 1, 2, 3, 4};
int result = functional::reduce(vector, [](constint current, constint prev) -> int {
return current + next;
}, 10);
std::cout << result << std::endl; // 20// Example 3:
std::vector<std::string> vector = {"0", "1", "2", "3", "4"};
auto result = functional::reduce(vector, [](const std::string current, const std::string prev) ->
std::string {
return current + next;
}
);
std::cout << result << std::endl; // 0, 1, 2, 3, 4return0;
}
functional::every(container, function);
/* * Tests whether all elements in the container pass the test implemented by the provided function. * * @returns {bool}*/
#include<vector>
#include<iostream>
#include"functional++.hpp"intmain () {
std::vector<int> vector = {0, 1, 2, 3, 4};
auto result = functional::every(vector, [](constint value) -> bool {
return value == 1;
});
std::cout << result << std::endl; // falsereturn0;
}
functional::some(container, function);
/* * Tests whether some element in the container passes the test implemented by the provided function. * * @returns {bool}*/
#include<vector>
#include<iostream>
#include"functional++.hpp"intmain () {
std::vector<int> vector = {0, 1, 2, 3, 4};
auto result = functional::every(vector, [](constint value) -> bool {
return value == 1;
});
std::cout << result << std::endl; // truereturn0;
}
functional::none(container, function);
/* * Tests whether none element in the container passes the test implemented by the provided function. * * @returns {bool}*/
#include<vector>
#include<iostream>
#include"functional++.hpp"intmain () {
std::vector<int> vector = {0, 1, 2, 3, 4};
auto result = functional::none(vector, [](constint value) -> bool {
return value == 5;
});
std::cout << result << std::endl; // truereturn0;
}
License
This library is licensed under the MIT license
About
functional++: is a small library with some functional algorithms