diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2025-11-06 21:12:44 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2025-11-06 21:12:44 -0800 |
| commit | cc8eeeb482f279c80ebef17fc5968e73a51b48b8 (patch) | |
| tree | ab1ae00741148e617af41c57a285e74a6cd5d43d /examples/branching.ts | |
| parent | ccaff310c85a64a852d96ee71ecf9640de57ea36 (diff) | |
add examples
Diffstat (limited to 'examples/branching.ts')
| -rw-r--r-- | examples/branching.ts | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/branching.ts b/examples/branching.ts new file mode 100644 index 0000000..e504f9d --- /dev/null +++ b/examples/branching.ts @@ -0,0 +1,24 @@ +/* +KaiScript is turing complete. Use the "?" function to branch execution. +Only the branch matching the condition will be evaluated. +*/ + +import { createFn } from "../src"; + +const myBranchingFn = createFn<[boolean]>()("fn(b, ?(b, 1, 0))"); + +// const result: 1 +const result = myBranchingFn(true); + +// This is very powerful! For example, if you're bad at addition, KaiScript can check your work +const isAdditionCorrect = createFn<[m: number, n: number, expected: number]>()( + `fn(m,n,e, ?(eq(add(m, n), e), "correct", "incorrect"))` +); + +// const badAddition: "incorrect" +const badAddition = isAdditionCorrect(5, 10, 2); +console.log(badAddition); + +// const goodAddition: "correct" +const goodAddition = isAdditionCorrect(5, 10, 15); +console.log(goodAddition); |
