Stack utilities

This commit is contained in:
Tony Garnock-Jones 2021-12-09 18:47:43 +01:00
parent e17fd13442
commit c9da4fcf26
1 changed files with 18 additions and 0 deletions

View File

@ -41,3 +41,21 @@ export function dropNonEmpty<T>(s: NonEmptyStack<T>, n: number): NonEmptyStack<T
}
return s;
}
export function depth<T>(s: Stack<T>): number {
let count = 0;
while (s !== null) {
count++;
s = s.rest;
}
return count;
}
export function toArray<T>(s: Stack<T>): T[] {
const result = [];
while (s !== null) {
result.push(s.item);
s = s.rest;
}
return result;
}