diff --git a/include/prelude.hpp b/include/prelude.hpp index 45a2b4f..d681529 100644 --- a/include/prelude.hpp +++ b/include/prelude.hpp @@ -606,6 +606,26 @@ namespace efp return result; } + // cartesian_for_index + + template + void cartesian_for_index(const F &f, const int &i) + { + for_index(f, i); + } + + template + void cartesian_for_index(const F &f, const int &i, const Ints &...is) + { + for (int i_ = 0; i_ < i; ++i_) + { + const auto inner = [=](const Ints &...is) + { f(i_, is...); }; + + cartesian_for_index(inner, is...); + } + } + // // todo begin // // todo end diff --git a/test/prelude_test.hpp b/test/prelude_test.hpp index 7531781..e2bce23 100644 --- a/test/prelude_test.hpp +++ b/test/prelude_test.hpp @@ -408,6 +408,16 @@ TEST_CASE("cartesian_map") CHECK(res == Array{1, 3, 2, 6}); } +TEST_CASE("cartesian_for_index") +{ + Vector res; + const auto f = [&](int a, int b) + { return res.push_back(a * b); }; + + cartesian_for_index(f, 2, 3); + CHECK(res == Vector{0, 0, 0, 0, 1, 2}); +} + // todo sequence view TEST_CASE("head")