export function takeWhile<T>(items: readonly T[], pred: (x: T) => boolean): T[] {
const out: T[] = [];
for (const x of items) {
if (!pred(x)) break;
out.push(x);
}
return out;
}
export function dropWhile<T>(items: readonly T[], pred: (x: T) => boolean): T[] {
let i = 0;
while (i < items.length && pred(items[i]!)) i++;
return items.slice(i);
}
takeWhile([1, 3, 5, 4, 7], n => n % 2 === 1); // [1, 3, 5]
dropWhile([1, 3, 5, 4, 7], n => n % 2 === 1); // [4, 7]
Create a free account and build your private vault. Share publicly whenever you want.