int main() { std::vector<int> vec{1, 5, 6, 7, 2, 3, 8, 3, 2, 1}; std::cout << "Greater than 4 (function pointer)\n"; for (auto i : filter(greater_than_four, vec)) { std::cout << i << '\n'; } std::cout << "Less than 4 (lambda)\n"; for (auto i : filter([] (const int i) { return i < 4; }, vec)) { std::cout << i << '\n'; } LessThanValue lv(4); std::cout << "Less than 4 (callable object)\n"; for (auto i : filter(lv, vec)) { std::cout << i << '\n'; } std::cout << "Nonzero ints filter(vec2)\n"; std::vector<int> vec2 {0, 1, 2, 0, 3, 0, 0, 0, 4, 5, 0}; for (auto i : filter(vec2)) { std::cout << i << '\n'; } std::cout << "odd numbers in range(10) temp\n"; for (auto i : filter([] (const int i) {return i % 2;}, iter::range(10))) { std::cout << i << '\n'; } std::cout << "range(-1, 2)\n"; for (auto i : filter(iter::range(-1, 2))) { std::cout << i << '\n'; } std::cout << "ever numbers in initializer_list\n"; for (auto i : filter([] (const int i) {return i % 2 == 0;}, {1, 2, 3, 4, 5, 6, 7})) { std::cout << i << '\n'; } std::cout << "default in initialization_list\n"; for (auto i : filter({-2, -1, 0, 0, 0, 1, 2})) { std::cout << i << '\n'; } std::cout << "ever numbers in vector temporary\n"; for (auto i : filter([] (const int i) {return i % 2 == 0;}, std::vector<int>{1, 2, 3, 4, 5, 6, 7})) { std::cout << i << '\n'; } return 0; }
auto dec_ten = [](MyUnMovable& el) -> MyUnMovable& { int va = el.get_val(); el.set_val(va - 10); return el; }; } TEST_CASE("filtering doesn't dereference multiple times", "[imap][filter]") { using iter::filter; using iter::imap; // source data std::array<MyUnMovable, 3> arr = {{{41}, {42}, {43}}}; auto transformed1 = imap(inc_ten, arr); auto filtered = filter( [](const MyUnMovable& el) { return 52 != el.get_val(); }, transformed1); auto transformed2 = imap(dec_ten, filtered); std::vector<int> v; for (auto&& el : transformed2) { // I would use imap again instead of the loop if this wasn't an imap // test v.push_back(el.get_val()); } std::vector<int> vc = {41, 43}; REQUIRE(v == vc); constexpr std::array<MyUnMovable, 3> arrc = {{{41}, {52}, {43}}}; REQUIRE(arr == arrc);
int compare_val; public: LessThanValue(int v) : compare_val(v) { } bool operator() (int i) { return i < this->compare_val; } }; } TEST_CASE("filter: handles different functor types", "[filter]") { Vec ns = {1,2, 5,6, 3,1, 7, -1, 5}; Vec vc = {1,2,3,1,-1}; SECTION("with function pointer") { auto f = filter(less_than_five, ns); Vec v(std::begin(f), std::end(f)); REQUIRE( v == vc ); } SECTION("with callable object") { auto f = filter(LessThanValue{5}, ns); Vec v(std::begin(f), std::end(f)); REQUIRE( v == vc ); } SECTION("with lambda") { auto ltf = [](int i) {return i < 5;}; auto f = filter(ltf, ns); Vec v(std::begin(f), std::end(f)); REQUIRE( v == vc );