diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/README.md | 5 | ||||
| -rw-r--r-- | examples/mapReduce.ts | 21 |
2 files changed, 25 insertions, 1 deletions
diff --git a/examples/README.md b/examples/README.md index 33c957d..1d0e0c1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,6 +1,7 @@ ## A guided tour of KaiScript through examples -Open this in your IDE of choice--make sure your LSP is running so you can inspect types. +Open this in your IDE of choice--make sure your LSP is running so you can inspect types. Feel free to run `tsx` on each file to verify the runtime outputs. +If you're reading this without an LSP, I've added type annotations in comments above each result. I promise these match what you'd see if you hovered over the value! 1. [Breaking the second wall](./mapper.ts) @@ -11,3 +12,5 @@ Open this in your IDE of choice--make sure your LSP is running so you can inspec 4. [Turing completeness](./branching.ts) 5. [Infinite computation](./recursion.ts) + +6. [Infinite transformation](./mapReduce.ts) diff --git a/examples/mapReduce.ts b/examples/mapReduce.ts new file mode 100644 index 0000000..3904b2a --- /dev/null +++ b/examples/mapReduce.ts @@ -0,0 +1,21 @@ +/* +You can do anything with map + reduce! +*/ + +import { createFn } from "../src"; + +const concatNumbers = createFn<[number[]]>()( + `fn(a, reduce(map(a, fn(n, tostring(n))), fn(acc, cur, add(acc,cur)), ""))` +); + +// const result: "1235" +const concatted = concatNumbers([1, 2, 35]); +console.log(concatted); + +const arrayLength = createFn<[any[]]>()( + `fn(a, reduce(a, fn(acc, add(acc, 1)), 0))` +); + +// const length: 2 +const length = arrayLength(["hello", "world"]); +console.log(length); |
