22 lines
799 B
TypeScript
22 lines
799 B
TypeScript
export function* zip<A, B>(a: Iterable<A>, b: Iterable<B>): Generator<[A, B], void, void> {
|
|
const iter_a = a[Symbol.iterator](), iter_b = b[Symbol.iterator]();
|
|
while (true) {
|
|
const next_a = next(iter_a), next_b = next(iter_b);
|
|
if (next_a === null || next_b === null) return;
|
|
yield [next_a, next_b];
|
|
}
|
|
}
|
|
|
|
export function next<T>(iterator: Iterator<T>) {
|
|
const result = iterator.next().value;
|
|
if (result === undefined) return null;
|
|
else return result as T;
|
|
}
|
|
|
|
export function split_once(text: string, separator: string) {
|
|
const cursor = text.indexOf(separator);
|
|
if (cursor === -1) return [text, null] as const;
|
|
const left = text.slice(0, cursor);
|
|
const right = text.slice(cursor + separator.length);
|
|
return [left, right] as const;
|
|
}
|