Skip to main content

Code Examples & Recipes

These snippets match the compiler's examples/ folder in Kyna. Standard-library functions are globals — no import std.*.


1. Queue + JSON​

const pending = createQueue();
enqueue(pending, "one");
enqueue(pending, "two");
const first = dequeue(pending);
const payload = jsonStringify({ first: first, empty: queueIsEmpty(pending) });

2. Stream a file copy​

try {
createDirectory("scratch");
writeFile("scratch/source.txt", "streamed");
const copied = copyFile("scratch/source.txt", "scratch/dest.txt");
print(copied);
} catch (failure) {
print(failure.message);
}

3. HTTP + parse IP​

try {
const res = await fetch("https://api.kyna.dev/v1/users/1");
if (res.ok) {
const data = res.json();
print(data["name"]);
print(parseIP("10.0.0.2"));
}
} catch (err) {
print(err.message);
}

4. Package layout​

app/
helpers.kyna
main.kyna
ky check ./app
ky run ./app

Sibling files share one namespace. Put unstable helpers under app/internal/ so other packages cannot import them (KSEM1042).

More runnable programs live in the compiler repo under examples/standard_library/, examples/network/, and examples/learning/.