diff options
Diffstat (limited to 'examples/guess-number.ts')
| -rw-r--r-- | examples/guess-number.ts | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/examples/guess-number.ts b/examples/guess-number.ts index a20c993..d08c616 100644 --- a/examples/guess-number.ts +++ b/examples/guess-number.ts @@ -1,33 +1,39 @@ -import { Print, PutString, Bind, Kind1, JsExpr, ReadLine, Do, PutStringLn } from '../stdlib' +import { + Print, + PutString, + Bind, + Kind1, + JsExpr, + ReadLine, + Do, + PutStringLn, +} from '../stdlib' export type main = [ - PutStringLn<"Guess a number between 0 & 9. You have 5 guesses">, - Bind< - JsExpr<"Math.floor(Math.random() * 10)">, - StartGuessing - > + PutStringLn<'Guess a number between 0 & 9. You have 5 guesses'>, + Bind<JsExpr<'Math.floor(Math.random() * 10)'>, StartGuessing> ] -interface AskForGuess<N extends number, Attempts extends 0[]> extends Kind1<string> { +interface AskForGuess<N extends number, Attempts extends 0[]> + extends Kind1<string> { return: `${this['input']}` extends `${N}` - ? PutStringLn<"Yay! You got it right!"> - : Do<[ - Print<`Wrong guess. Total attempts: ${ - [...Attempts, 0] extends infer Ls extends 0[] ? Ls['length'] : 0 - }/5`>, - (StartGuessing<[...Attempts, 0]> & { input: N })['return'], - ]> + ? PutStringLn<'Yay! You got it right!'> + : Do< + [ + PutString<'Wrong guess. Total attempts'>, + Print<`${[...Attempts, 0] extends infer Ls extends 0[] + ? Ls['length'] + : 0}/5`>, + (StartGuessing<[...Attempts, 0]> & { input: N })['return'] + ] + > } interface StartGuessing<Attempts extends 0[] = []> extends Kind1<number> { return: Attempts['length'] extends 5 - ? PutStringLn<"Max attempts exceeded. Game over!"> + ? PutStringLn<'Max attempts exceeded. Game over!'> : Bind< - Do<[ - PutString<"Your guess? ">, - ReadLine - ]>, - AskForGuess<this['input'], Attempts> - > + Do<[PutString<'Your guess? '>, ReadLine]>, + AskForGuess<this['input'], Attempts> + > } - |
