@lou.codes/iterables
Version:
🔁 Iterable and AsyncIterable utils
25 lines (24 loc) • 681 B
JavaScript
import { createIterableIterator } from "./createIterableIterator.js";
import { filter } from "./filter.js";
/**
* Drop the specified amount of items from the given iterable.
*
* @category Generators
* @example
* ```typescript
* const drop2 = drop(2);
* drop2([1, 2, 3, 4, 5]); // [3, 4, 5]
* ```
* @param amount Amount of items to drop.
* @returns Curried function with `amount` in context.
*/
export const drop = amount => {
const amountFilter = iterable => {
let count = -1n;
return filter(() => (count += 1n) >= amount)(iterable);
};
return iterable =>
createIterableIterator(function* () {
yield* amount > 0 ? amountFilter(iterable) : iterable;
});
};