Skip to content

Commit

Permalink
Merge pull request #154 from elbeno/variadic-seq
Browse files Browse the repository at this point in the history
🎨 Make `seq` variadic
  • Loading branch information
lukevalenty authored Feb 25, 2025
2 parents 2a05a47 + c6166d8 commit 396978b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 12 additions & 8 deletions include/async/sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,18 @@ template <stdx::ct_string Name = "sequence", sender S, std::invocable F>
return std::forward<S>(s) | sequence<Name>(std::forward<F>(f));
}

template <stdx::ct_string Name = "seq", sender S>
[[nodiscard]] constexpr auto seq(S &&s) {
return sequence<Name>(_sequence::detail::wrapper{std::forward<S>(s)});
}

template <stdx::ct_string Name = "seq", sender S1, sender S2>
[[nodiscard]] constexpr auto seq(S1 &&s1, S2 &&s2) -> sender auto {
return std::forward<S1>(s1) | seq<Name>(std::forward<S2>(s2));
template <stdx::ct_string Name = "seq", sender... S>
requires(sizeof...(S) > 0)
[[nodiscard]] constexpr auto seq(S &&...s) {
if constexpr (sizeof...(S) == 1) {
return sequence<Name>(
_sequence::detail::wrapper{std::forward<S>(s)...});
} else {
return []<typename S1, typename... Ss>(S1 &&s1, Ss &&...ss) {
return (std::forward<S1>(s1) | ... |
seq<Name>(std::forward<Ss>(ss)));
}(std::forward<S>(s)...);
}
}

struct sequence_t;
Expand Down
8 changes: 8 additions & 0 deletions test/sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ TEST_CASE("seq(s1, s2) is the same as s1 | seq(s2)", "[sequence]") {
CHECK(value == 42);
}

TEST_CASE("seq is variadic", "[sequence]") {
int value{};
auto s = async::seq(async::just(), async::just(42), async::just(17));
auto op = async::connect(s, receiver{[&](auto i) { value = i; }});
async::start(op);
CHECK(value == 17);
}

TEST_CASE("seq(sender) with move-only sender", "[sequence]") {
int value{};
auto s = async::just() | async::seq(async::just(move_only{42}));
Expand Down

0 comments on commit 396978b

Please sign in to comment.