int main() { std::vector<int> vec1 = {1, 2, 3, 4, 5, 6}; std::vector<int> vec2 = {10, 20, 30, 40, 50, 60}; for (auto i : imap([] (int x, int y) { return x + y; }, vec1, vec2)) { std::cout << i << '\n'; } std::vector<int> vec3 = {100, 200, 300, 400, 500, 600}; for (auto i : imap([] (int a, int b, int c) { return a + b + c; }, vec1, vec2, vec3)) { std::cout << i << '\n'; } for (auto i : imap([] (int i) { return i * i; }, vec1)) { std::cout << i << '\n'; } std::vector<int> vec{1, 2, 3, 4, 5}; for (auto i : imap([] (int x) { return x * x; }, vec)) { std::cout << i << '\n'; } std::vector<int> vec4{1, 2, 3}; for (auto i : imap([] (int a, int b) { return a + b; }, vec, vec4)) { 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);
} }; int power(int b, int e) { int acc = 1; for (int i = 0; i < e; ++i) { acc *= b; } return acc; } } TEST_CASE("imap: works with lambda, callable, and function", "[imap]") { Vec ns = {10, 20, 30}; SECTION("with lambda") { auto im = imap([](int i) { return i + 1; }, ns); Vec v(std::begin(im), std::end(im)); Vec vc = {11, 21, 31}; REQUIRE( v == vc ); } SECTION("with function") { auto im = imap(plusone, ns); Vec v(std::begin(im), std::end(im)); Vec vc = {11, 21, 31}; REQUIRE( v == vc ); } SECTION("with function") { auto im = imap(PlusOner{}, ns); Vec v(std::begin(im), std::end(im));