@@ -155,6 +155,39 @@ export function take<T>(it: Iterable<T>, n: number): IterableCircular<T> {
155
155
} ;
156
156
}
157
157
158
+ /**
159
+ * Returns a new iterable containing the items of `it` except the first `n` items.
160
+ * @param it - The iterable being taken from.
161
+ * @param n - The number of items to drop.
162
+ * @typeParam T - The type of items in both `it` and the returned iterable.
163
+ * @returns A new iterable of `it` which skips the first `n` items.
164
+ * @example
165
+ * ```ts
166
+ * import * as iter from "https://deno.land/x/iter/mod.ts";
167
+ *
168
+ * const numbers = iter.create.range(1, 10);
169
+ * const from7 = iter.drop(numbers, 6);
170
+ *
171
+ * for (const num of from7) {
172
+ * console.log(num);
173
+ * }
174
+ *
175
+ * // -> 7
176
+ * // -> 8
177
+ * // -> 9
178
+ * // -> 10
179
+ * ```
180
+ */
181
+ export function drop < T > ( it : Iterable < T > , n : number ) : IterableCircular < T > {
182
+ return {
183
+ * [ Symbol . iterator ] ( ) {
184
+ const iterator = it [ Symbol . iterator ] ( ) ;
185
+ for ( let i = 0 ; i < n ; i ++ ) iterator . next ( ) ;
186
+ yield * { [ Symbol . iterator ] : ( ) => iterator } ;
187
+ } ,
188
+ } ;
189
+ }
190
+
158
191
/**
159
192
* Returns a new iterable which yields until `f` returns true.
160
193
*
@@ -208,6 +241,126 @@ export function until<T>(
208
241
} ;
209
242
}
210
243
244
+ /**
245
+ * Returns a new iterable which skips items from `it` until `f` returns true.
246
+ * true.
247
+ * @param it - The iterable being skipped.
248
+ * @param {IterablePredicateCallback } f - A function that accepts up to three
249
+ * arguments. The `dropUntil` function calls `f` one time for each item
250
+ * in the iterable until `f` returns true.
251
+ * @param includeFirst - Whether the item for which `f` returns true should be
252
+ * included.
253
+ * @typeParam T - The type of items in both `it` and the returned iterable.
254
+ * @returns A new iterable of `it` which begins at the first element where `f` returns true
255
+ * @example
256
+ * ```ts
257
+ * import * as iter from "https://deno.land/x/iter/mod.ts";
258
+ *
259
+ * const numbers = iter.create.range(1, 10);
260
+ * const dropped = iter.dropUntil(numbers, (n) => n >= 5);
261
+ *
262
+ * for (const num of dropped) {
263
+ * console.log(num);
264
+ * }
265
+ *
266
+ * // -> 5
267
+ * // -> 6
268
+ * // -> 7
269
+ * // -> 8
270
+ * // -> 9
271
+ * // -> 10
272
+ * ```
273
+ */
274
+ export function dropUntil < T > (
275
+ it : Iterable < T > ,
276
+ f : IterablePredicateCallback < T > ,
277
+ includeFirst = true ,
278
+ ) : IterableCircular < T > {
279
+ return {
280
+ * [ Symbol . iterator ] ( ) {
281
+ let index = 0 ;
282
+ let dropping = true ;
283
+ for ( const item of it ) {
284
+ if ( dropping ) {
285
+ dropping = ! f ( item , index ++ , it ) ;
286
+ if ( dropping || ! includeFirst ) continue ;
287
+ }
288
+ yield item ;
289
+ }
290
+ } ,
291
+ } ;
292
+ }
293
+
294
+ /**
295
+ * Returns a new iterable which yields while `f` returns true.
296
+ *
297
+ * @param it - The iterable being cut.
298
+ * @param {IterablePredicateCallback } f - A function that accepts up to three
299
+ * arguments. The `takeWhile` function calls `f` one time for each item in the iterable.
300
+ * included.
301
+ * @typeParam T - The type of items in both `it` and the returned iterable.
302
+ * @returns A new iterables of `it` which terminates
303
+ * @example
304
+ * ```ts
305
+ * import * as iter from "https://deno.land/x/iter/mod.ts";
306
+ *
307
+ * const naturals = iter.create.increments(1);
308
+ * const numbers = iter.takeWhile(naturals, (n) => n <= 5);
309
+ *
310
+ * for (const num of numbers) {
311
+ * console.log(num);
312
+ * }
313
+ *
314
+ * // -> 1
315
+ * // -> 2
316
+ * // -> 3
317
+ * // -> 4
318
+ * // -> 5
319
+ * ```
320
+ */
321
+ export function takeWhile < T > (
322
+ it : Iterable < T > ,
323
+ f : IterablePredicateCallback < T > ,
324
+ ) : IterableCircular < T > {
325
+ return until ( it , ( ...args ) => ! f ( ...args ) , false ) ;
326
+ }
327
+
328
+ /**
329
+ * Returns a new iterable which skips items from `it` while `f` returns true.
330
+ * true.
331
+ * @param it - The iterable being skipped.
332
+ * @param {IterablePredicateCallback } f - A function that accepts up to three
333
+ * arguments. The `dropWhile` function calls `f` one time for each item
334
+ * in the iterable.
335
+ *
336
+ * @typeParam T - The type of items in both `it` and the returned iterable.
337
+ * @returns A new iterable of `it` which begins at the first element where `f` returns false
338
+ * @example
339
+ * ```ts
340
+ * import * as iter from "https://deno.land/x/iter/mod.ts";
341
+ *
342
+ * const numbers = iter.create.range(1, 10);
343
+ * const dropped = iter.dropWhile(numbers, (n) => n < 5);
344
+ *
345
+ * for (const num of dropped) {
346
+ * console.log(num);
347
+ * }
348
+ *
349
+ * // -> 5
350
+ * // -> 6
351
+ * // -> 7
352
+ * // -> 8
353
+ * // -> 9
354
+ * // -> 10
355
+ * ```
356
+ */
357
+ export function dropWhile < T > (
358
+ it : Iterable < T > ,
359
+ f : IterablePredicateCallback < T > ,
360
+ ) : IterableCircular < T > {
361
+ return dropUntil ( it , ( ...args ) => ! f ( ...args ) ) ;
362
+ }
363
+
211
364
/**
212
365
* Returns the items of an iterable that meet the condition specified in a
213
366
* callback function.
0 commit comments