diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-01-11 16:02:26 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-01-11 16:02:26 +0530 |
| commit | 49d6379c3d40b684b28f4a957ff1ffb81a2560ee (patch) | |
| tree | fac62b2de1670ecfea6496949b028fa8fe0fe444 /examples | |
| parent | a6ff70d09e1e5f161e401a3665bfe44b2abfc44e (diff) | |
| download | ts-types-lang-49d6379c3d40b684b28f4a957ff1ffb81a2560ee.tar.gz ts-types-lang-49d6379c3d40b684b28f4a957ff1ffb81a2560ee.zip | |
refactor: minor refactor all around
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/custom-effect.ts | 20 | ||||
| -rw-r--r-- | examples/ffi.ts | 19 | ||||
| -rw-r--r-- | examples/greeting.ts | 4 | ||||
| -rw-r--r-- | examples/guess-number.ts | 6 | ||||
| -rw-r--r-- | examples/test-runner.ts | 48 |
5 files changed, 44 insertions, 53 deletions
diff --git a/examples/custom-effect.ts b/examples/custom-effect.ts deleted file mode 100644 index 74c96b0..0000000 --- a/examples/custom-effect.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Bind, Effect, Kind1 } from '../stdlib/effect' -import { DefineEffect } from '../stdlib/sys' -import { Print } from '../stdlib/stdio' - -interface Mathemagic<_A, _B> extends Effect {} - -interface PrintK extends Kind1 { - return: Print<this['input']> -} - -export type main = [ - DefineEffect< - 'Mathemagic', - `(a, b) => { - console.log(typeToString(a)) - return { result: 5 * b.getLiteralValue() } - }` - >, - Bind<Mathemagic<'a', 69>, PrintK> -] diff --git a/examples/ffi.ts b/examples/ffi.ts index f118085..a09aeef 100644 --- a/examples/ffi.ts +++ b/examples/ffi.ts @@ -6,19 +6,24 @@ interface PrintK<Label extends string = ''> extends Kind1 { return: Debug<Label, this['input']> } -type Square<N extends number> = JsExpr<`${N} ** 2`> +// Declare custom effect +interface CustomThingy<_A, _B extends number> extends Effect {} -interface Mathemagic<_A, _B extends number> extends Effect {} +// Alias for squaring a number +type Square<N extends number> = JsExpr<`${N} ** 2`> export type main = [ + // FFI via js expression Bind<JsExpr<'{ boobaa: [5 * 3, 5 * 2] }'>, PrintK<'|'>>, Bind<Square<69>, PrintK<'69^2 ='>>, + + // FFI via custom effect DefineEffect< - 'Mathemagic', - `(a, b) => { - console.log(typeToString(a)) - return { result: 5 * b.getLiteralValue() } + 'CustomThingy', + `([a, b], ctx) => { + console.log(ctx.typeToString(a)) + return { result: 5 * ctx.getTypeValue(b) } }` >, - Bind<Mathemagic<'a', 69>, PrintK<'69 * 5 ='>> + Bind<CustomThingy<'a', 69>, PrintK<'69 * 5 ='>> ] diff --git a/examples/greeting.ts b/examples/greeting.ts index e4c2161..d3d755f 100644 --- a/examples/greeting.ts +++ b/examples/greeting.ts @@ -1,8 +1,8 @@ -import { Bind, Kind1, Seq } from '../stdlib/effect' +import { Bind, Kind1 } from '../stdlib/effect' import { PutString, ReadLine, PutStringLn } from '../stdlib/stdio' interface GreetK extends Kind1<string> { - return: Seq<[PutString<'Hello, '>, PutString<`${this['input']}\n`>]> + return: PutStringLn<`Hello, ${this['input']}`> } export type main = [ diff --git a/examples/guess-number.ts b/examples/guess-number.ts index 169805f..45d60e6 100644 --- a/examples/guess-number.ts +++ b/examples/guess-number.ts @@ -7,15 +7,15 @@ export type main = [ Bind<JsExpr<'Math.floor(Math.random() * 10)'>, StartGuessing> ] +type Len<Ls extends any[]> = Ls['length'] extends number ? Ls['length'] : 0 + interface AskForGuess<N extends number, Attempts extends 0[]> extends Kind1<string> { return: `${this['input']}` extends `${N}` ? PutStringLn<'Yay! You got it right!'> : Do< [ PutString<'Wrong guess. Total attempts'>, - PutStringLn<` ${[...Attempts, 0] extends infer Ls extends 0[] - ? Ls['length'] - : 0}/5`>, + PutStringLn<` ${Len<[...Attempts, 0]>}/5`>, (StartGuessing<[...Attempts, 0]> & { input: N })['return'] ] > diff --git a/examples/test-runner.ts b/examples/test-runner.ts index 7093bf7..fecd3e4 100644 --- a/examples/test-runner.ts +++ b/examples/test-runner.ts @@ -1,39 +1,45 @@ import { Do, Effect } from '../stdlib/effect' -import { Print, PutStringLn } from '../stdlib/stdio' +import { PutStringLn } from '../stdlib/stdio' import { DefineEffect } from '../stdlib/sys' -type Testi<m extends string, effs extends Effect[]> = [ - PutStringLn<m>, +type Test<m extends string, effs extends Effect[]> = [ + PutStringLn<`* ${m}`>, ...effs, ] type Equals<Left, Right> = [Left] extends [Right] ? ([Right] extends [Left] ? true : false) : false +type Not<B extends boolean> = B extends true ? false : true -type CompileTimeErrors = false -interface Assert<_B extends (CompileTimeErrors extends true ? true : boolean)> extends Effect { } +interface TestConfig { + CompileTestFailures: false +} -type testCases = [ - ...Testi<"should do some stuff", [ - Assert<Equals<1, 2>>, - ]>, - - ...Testi<"hello world", [ - Print<2>, - ]>, - - ...Testi<"should do some other stuff", [ - Print<5>, - ]>, -] +type Assertion = TestConfig['CompileTestFailures'] extends true ? true : boolean +interface Assert<_B extends Assertion> extends Effect { } export type main = [ - DefineEffect<'Assert', `(b) => { - if (!b.getLiteralValue()) { + DefineEffect<'Assert', `([b], ctx) => { + if (!ctx.getTypeValue(b)) { throw new Error('AAAAAAA') } }`>, + PutStringLn<"Running tests...">, - Do<testCases>, + + Do<[ + ...Test<"should do some stuff", [ + Assert<Equals<1, 1>>, + Assert<Not<Equals<2, 1>>>, + ]>, + + ...Test<"hello world", [ + Assert<Equals<1, 1>>, + ]>, + + ...Test<"should do some other stuff", [ + Assert<Equals<1, 1>>, + ]>, + ]>, ] |
