Skip to content

Commit

Permalink
feat: Implement cartesian_for_index with test
Browse files Browse the repository at this point in the history
  • Loading branch information
cwahn committed Sep 18, 2023
1 parent 2983160 commit 07803aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/prelude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,26 @@ namespace efp
return result;
}

// cartesian_for_index

template <typename F = void (*)(const int &)>
void cartesian_for_index(const F &f, const int &i)
{
for_index(f, i);
}

template <typename... Ints, typename F = void (*)(const int &)>
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
Expand Down
10 changes: 10 additions & 0 deletions test/prelude_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,16 @@ TEST_CASE("cartesian_map")
CHECK(res == Array<int, 4>{1, 3, 2, 6});
}

TEST_CASE("cartesian_for_index")
{
Vector<int> res;
const auto f = [&](int a, int b)
{ return res.push_back(a * b); };

cartesian_for_index(f, 2, 3);
CHECK(res == Vector<int>{0, 0, 0, 0, 1, 2});
}

// todo sequence view

TEST_CASE("head")
Expand Down

0 comments on commit 07803aa

Please sign in to comment.