diff options
Diffstat (limited to 'examples/createFn.ts')
| -rw-r--r-- | examples/createFn.ts | 24 |
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); |
