summaryrefslogtreecommitdiff
path: root/examples/createFn.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/createFn.ts
parentccaff310c85a64a852d96ee71ecf9640de57ea36 (diff)
add examples
Diffstat (limited to 'examples/createFn.ts')
-rw-r--r--examples/createFn.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/createFn.ts b/examples/createFn.ts
new file mode 100644
index 0000000..4f8e22f
--- /dev/null
+++ b/examples/createFn.ts
@@ -0,0 +1,24 @@
+/*
+Writing and immediately evaluating KaiScript programs is of little use.
+You'll find the most benefit from its native interoperability with TypeScript.
+*/
+
+// createFn receives a KaiScript program and evaluates it. If it evaluates to a function,
+// it will return a generic TypeScript function corresponding to this function.
+import { createFn } from "../src";
+
+// We define a function F(n) -> n
+const myFn = createFn()(`fn(n, n)`);
+
+// const result: 5
+const result = myFn(5);
+console.log(result);
+
+// We can use this to define useful functions, and restrict their arguments
+const myUsefulFn = createFn<[number, number]>()(
+ `fn(m, n, add(tostring(m), "*", tostring(n), "=", tostring(mul(m,n))))`
+);
+
+// const usefulResult: "2*5=10"
+const usefulResult = myUsefulFn(2, 5);
+console.log(usefulResult);