From 4d82623f191042c362ddd2ad5e8ded99d5854169 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 6 Jan 2023 21:12:53 +0530 Subject: feat: adds then effect + fixes guess number example --- examples/guess-number.ts | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/guess-number.ts b/examples/guess-number.ts index aca3915..8f5d535 100644 --- a/examples/guess-number.ts +++ b/examples/guess-number.ts @@ -1,15 +1,33 @@ -import { Print, PutString, Bind, Kind1, JsExpr, ReadLine, Seq } from '../src/stdlib' +import { Print, PutString, Bind, Kind1, JsExpr, ReadLine, Then, PutStringLn } from '../src/stdlib' -export type main = Bind< - JsExpr<"Math.floor(Math.random() * 10)">, - StartGuessing -> +export type main = [ + PutStringLn<"You have 5 guesses">, + Bind< + JsExpr<"Math.floor(Math.random() * 10)">, + StartGuessing + > +] -interface AskForGuess extends Kind1 { - return: this['input'] extends `${N}` ? Print<"Yaya"> : Print<"naaah"> +interface AskForGuess extends Kind1 { + return: `${this['input']}` extends `${N}` + ? PutStringLn<"Yay! You got it right!"> + : Then<[ + Print<`Wrong guess. Total attempts: ${ + [...Attempts, 0] extends infer Ls extends 0[] ? Ls['length'] : 0 + }`>, + (StartGuessing<[...Attempts, 0]> & { input: N })['return'], + ]> } -interface StartGuessing extends Kind1 { - return: Bind, ReadLine]>, AskForGuess> +interface StartGuessing extends Kind1 { + return: Attempts['length'] extends 5 + ? PutStringLn<"Max attempts exceeded. Game over!"> + : Bind< + Then<[ + PutString<"Your guess? ">, + ReadLine + ]>, + AskForGuess + > } -- cgit v1.3.1