summaryrefslogtreecommitdiff
path: root/examples/createFn.ts
blob: 4f8e22fc6b3379ce331b194f1e4077c3c2fa7587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);