aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-01-06 21:12:53 +0530
committerAkshay Nair <phenax5@gmail.com>2023-01-06 21:12:53 +0530
commit4d82623f191042c362ddd2ad5e8ded99d5854169 (patch)
treec41457c79b1cacfbb7ebd75ec1f76121c5f1b8e4 /examples
parentcda0419722b022ed0a8f3c4fd3122230b19a0c64 (diff)
downloadts-types-lang-4d82623f191042c362ddd2ad5e8ded99d5854169.tar.gz
ts-types-lang-4d82623f191042c362ddd2ad5e8ded99d5854169.zip
feat: adds then effect + fixes guess number example
Diffstat (limited to 'examples')
-rw-r--r--examples/guess-number.ts36
1 files changed, 27 insertions, 9 deletions
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<N extends number> extends Kind1<string> {
- return: this['input'] extends `${N}` ? Print<"Yaya"> : Print<"naaah">
+interface AskForGuess<N extends number, Attempts extends 0[]> extends Kind1<string> {
+ 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<number> {
- return: Bind<Seq<[PutString<"Your guess? ">, ReadLine]>, AskForGuess<this['input']>>
+interface StartGuessing<Attempts extends 0[] = []> extends Kind1<number> {
+ return: Attempts['length'] extends 5
+ ? PutStringLn<"Max attempts exceeded. Game over!">
+ : Bind<
+ Then<[
+ PutString<"Your guess? ">,
+ ReadLine
+ ]>,
+ AskForGuess<this['input'], Attempts>
+ >
}