From cc8eeeb482f279c80ebef17fc5968e73a51b48b8 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Thu, 6 Nov 2025 21:12:44 -0800 Subject: add examples --- examples/createFn.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/createFn.ts (limited to 'examples/createFn.ts') 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); -- cgit v1.2.3-70-g09d2