Skip to content

Commit

Permalink
Flatten: avoid hard error with non-sequence inner
Browse files Browse the repository at this point in the history
Sequence flatten requires both outer and inner to be sequences
  • Loading branch information
tcbrindle committed Dec 9, 2024
1 parent 2496980 commit 588ef04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/flux/adaptor/flatten.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct flatten_adaptor : inline_iter_base<flatten_adaptor<Base>> {
};

template <sequence Base>
requires sequence<element_t<Base>>
struct flatten_adaptor<Base> : inline_iter_base<flatten_adaptor<Base>> {
private:
using InnerSeq = element_t<Base>;
Expand Down
29 changes: 28 additions & 1 deletion test/test_flatten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
* * the element type of the outer sequence is a reference type
* * the inner sequence is multipass
*
* Otherwise, the single-pass version is used.
* Otherwise, the single-pass version is used when the outer and
* the inner are both sequences, else the basic iterable version
* is used.
*/

namespace {

constexpr bool test_flatten_iterable()
{
// Outer is iterable only
{
std::array<std::array<int, 3>, 3> arr{
std::array{1, 2, 3},
Expand All @@ -49,6 +52,30 @@ constexpr bool test_flatten_iterable()
using F = decltype(flattened);
static_assert(flux::iterable<F>);
static_assert(flux::iterable<F const>);
static_assert(not flux::sequence<F>);

STATIC_CHECK(flattened.all(flux::pred::positive));
STATIC_CHECK(check_equal(flattened, {1, 2, 3, 4, 5, 6, 7, 8, 9}));
}

// Outer is multipass, inner is iterable only
{
std::array<std::array<int, 3>, 3> arr{
std::array{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

auto views = flux::map(arr, [](auto inner) {
return std::views::transform(std::move(inner), std::identity{});
});

auto flattened = flux::ref(views).flatten();

using F = decltype(flattened);
static_assert(flux::iterable<F>);
static_assert(flux::iterable<F const>);
static_assert(not flux::sequence<F>);

STATIC_CHECK(flattened.all(flux::pred::positive));
STATIC_CHECK(check_equal(flattened, {1, 2, 3, 4, 5, 6, 7, 8, 9}));
Expand Down

0 comments on commit 588ef04

Please sign in to comment.