summaryrefslogtreecommitdiff
path: root/examples/branching.ts
diff options
context:
space:
mode:
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);