Created
May 7, 2017 17:05
-
-
Save AyaMorisawa/6db50ee5fc9edbc7913ff25381f15912 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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