summaryrefslogtreecommitdiff
path: root/examples/branching.ts
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2025-11-06 21:12:44 -0800
committerKai Stevenson <kai@kaistevenson.com>2025-11-06 21:12:44 -0800
commitcc8eeeb482f279c80ebef17fc5968e73a51b48b8 (patch)
treeab1ae00741148e617af41c57a285e74a6cd5d43d /examples/branching.ts
parentccaff310c85a64a852d96ee71ecf9640de57ea36 (diff)
add examples
Diffstat (limited to 'examples/branching.ts')
-rw-r--r--examples/branching.ts24
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);