Skip to content

Instantly share code, notes, and snippets.

@AyaMorisawa
Created May 7, 2017 17:05
Show Gist options
  • Save AyaMorisawa/6db50ee5fc9edbc7913ff25381f15912 to your computer and use it in GitHub Desktop.
Save AyaMorisawa/6db50ee5fc9edbc7913ff25381f15912 to your computer and use it in GitHub Desktop.
class TailRec<T> {
private next: () => TailRec<T>;
private done: boolean;
private result: T;
constructor(next: () => TailRec<T>, done: boolean, result: T) {
this.next = next;
this.done = done;
this.result = result;
}
get(): T {
let t: TailRec<T> = this;
while (true) {
if (t.done) {
return t.result;
}
t = t.next();
}
}
static call<T>(next: () => TailRec<T>): TailRec<T> {
return new TailRec<T>(next, false, null);
}
static done<T>(result: T): TailRec<T> {
return new TailRec<T>(() => null, true, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment