@@ -64,8 +64,8 @@ generator<std::tuple<T1, T2>> zip(generator<T1> gen1, generator<T2> gen2) {
6464 * \tparam T The type contained in the generator.
6565 * \tparam Pred The function type of the predicate function. Should return a
6666 * bool.
67- * \param gen The generator containing the original values.
68- * \param p The predicate.
67+ * \param[in,out] gen The generator containing the original values.
68+ * \param[in] p The predicate.
6969 * \returns A new generator which yields all values in the original generator
7070 * except those not matching the predicate.
7171 */
@@ -79,6 +79,19 @@ generator<T> filter(generator<T> gen, Pred p) {
7979 co_return ;
8080}
8181
82+ /* *
83+ * \brief Ignores the first n elements from a generator.
84+ *
85+ * Extracts the first `count` elements from the generator and throws those
86+ * away. All subsequent elements in the generator are passed through as normal.
87+ * If there aren't enough elements in the original generator, an empty generator
88+ * is returned.
89+ *
90+ * \tparam T The type contained in the generator.
91+ * \param[in,out] gen The generator to drop from.
92+ * \param[in] count The amount of elements to ignore.
93+ * \returns A new generator yielding all values except the first n.
94+ */
8295template <typename T> generator<T> drop (generator<T> gen, size_t count) {
8396 for (size_t i = 0 ; i < count && gen; i++) {
8497 gen ();
@@ -90,13 +103,42 @@ template <typename T> generator<T> drop(generator<T> gen, size_t count) {
90103 co_return ;
91104}
92105
106+ /* *
107+ * \brief Yields the first n elements from a generator.
108+ *
109+ * Extracts and yields exactly `count` elements (or less if the generator
110+ * doesn't contain that much values). The iteration is then stopped and no
111+ * further elements will be generated. If generation has side effects, the side
112+ * effects of elements after the first n will not be observable.
113+ *
114+ * \tparam T The type contained in the generator.
115+ * \param[in,out] gen The generator to take elements from.
116+ * \param[in] count The amount of elements to yield.
117+ * \returns A new generator yielding only the first n values.
118+ */
93119template <typename T> generator<T> take (generator<T> gen, size_t count) {
94120 for (size_t i = 0 ; i < count && gen; i++) {
95121 co_yield gen ();
96122 }
97123 co_return ;
98124}
99125
126+ /* *
127+ * \brief Drops elements while they satisfy a certain predicate.
128+ *
129+ * Iterates over the elements in the generator and drops them until it
130+ * encounters a value not satisfying the predicate (`!p(value)` is true). Then
131+ * it starts yielding each remaining value in the generator. To remove all
132+ * values satisfying `p`, use `fpgen::filter(gen, [&p](auto v) { return !p(v);
133+ * });`.
134+ *
135+ * \tparam T The type contained in the generator.
136+ * \tparam Pred The type of the predicate (should be a T -> bool function).
137+ * \param gen The generator to drop elements from.
138+ * \param p The predicate.
139+ * \returns A new generator where the first element is guaranteed to not
140+ * satisfy `p`.
141+ */
100142template <typename T, typename Pred>
101143generator<T> drop_while (generator<T> gen, Pred p) {
102144 while (gen) {
@@ -113,6 +155,23 @@ generator<T> drop_while(generator<T> gen, Pred p) {
113155 co_return ;
114156}
115157
158+ /* *
159+ * \brief Yields elements while they satisfy a certain predicate.
160+ *
161+ * Iterates over the elements in the generator and yields them until it
162+ * encounters a value not satisfying the predicate (`!p(value)`). Then it stops
163+ * generating (any side effects from the subsequent elements won't be
164+ * observable). To obtain a generator with all elements satisfying `p`, use
165+ * `fpgen::filter(gen, p);` instead.
166+ *
167+ * \tparam T The type contained in the generator.
168+ * \tparam Pred The type of the predicate (should be a T -> bool function).
169+ * \param gen The generator to take elements from.
170+ * \param p The predicate.
171+ * \returns A new generator where all elements are guaranteed to satisfy the
172+ * predicate and were generated before any element which didn't satisfy the
173+ * predicate.
174+ */
116175template <typename T, typename Pred>
117176generator<T> take_while (generator<T> gen, Pred p) {
118177 while (gen) {
0 commit comments