Zekken Documentation

Ready-to-use modules for the things scripts actually do: compute, move data, and interact with the system.

Section 5 of 8

Libraries

Import Forms#

use os; // Import the whole library

// Import only the functions you will need
use { sqrt, pow } from math;
use { read_file, write_file } from fs;

Math Library

Type notes: Most math functions accept numeric values (either int or float). When a function returns a float, it returns float even if you pass int.

Constants:

  • math.PI, math.E, math.I
use math;
@println => |"PI: " + math.PI| // 3.141592653589793
@println => |"E: " + math.E|   // 2.718281828459045
@println => |"I: " + math.I|   // 0 + 1i

Functions:

use math;
let sqrt_val: float = math.sqrt => |16.0|; // 4.0
@println => |"sqrt(16): " + sqrt_val|
use math;
let pow_val: float = math.pow => |2.0, 8.0|; // 256.0
@println => |"pow(2, 8): " + pow_val|
use math;
let abs_val: float = math.abs => |-42.0|; // 42.0
@println => |"abs(-42): " + abs_val|
use math;
let out: float = math.sin => |math.PI / 2.0|; // 1.0
@println => |out|
use math;
let out: float = math.cos => |0.0|; // 1.0
@println => |out|
use math;
let out: float = math.tan => |0.0|; // 0.0
@println => |out|
use math;
let out: float = math.log => |math.E|; // 1.0
@println => |out|
use math;
let out: float = math.log => |1000.0, 10.0|;
@println => |out|
use math;
let exp_val: float = math.exp => |1.0|; // 2.7182818284590455
@println => |"exp(1): " + exp_val|
use math;
let floor_val: float = math.floor => |3.9|; // 3.0
@println => |"floor(3.9): " + floor_val|
use math;
let ceil_val: float = math.ceil => |3.1|; // 4.0
@println => |"ceil(3.1): " + ceil_val|
use math;
let round_val: float = math.round => |3.6|; // 4.0
@println => |"round(3.6): " + round_val|
use math;
let min_val: float = math.min => |7.5, 2.25|; // 2.25
@println => |"min(7.5, 2.25): " + min_val|
use math;
let max_val: float = math.max => |7.5, 2.25|; // 7.5
@println => |"max(7.5, 2.25): " + max_val|
use math;
let clamp_val: float = math.clamp => |12.0, 0.0, 10.0|; // 10.0
@println => |"clamp(12, 0, 10): " + clamp_val|

let clamp_val: float = math.clamp => |-12.0, 0.0, 10.0|; // 0.0
@println => |"clamp(-12, 0, 10): " + clamp_val|
use math;
let rand_val: float = math.random => ||;
@println => |"random(): " + rand_val|
use math;
let rand_int_val: int = math.rand_int => |1, 10|;
@println => |"rand_int(1, 10): " + rand_int_val|
use math;
let choice_val: int = math.rand_choice => |[10, 20, 30, 40]|;
@println => |"rand_choice([10,20,30,40]): " + choice_val|
use math;
let shuffled_vals: arr = math.shuffle => |[1, 2, 3, 4, 5]|;
@println => |"shuffle([1,2,3,4,5]): " + shuffled_vals|
use math;
let atan2_val: float = math.atan2 => |1.0, 1.0|;
@println => |"atan2(1, 1): " + atan2_val|
use math;
let vector: arr = math.vector => |[1, 2, 3]|;
@println => |"vector: " + vector|
use math;
let v1: arr = math.vector => |[1, 2, 3]|;
let v2: arr = math.vector => |[4, 5, 6]|;

let dot_val: float = math.dot => |v1, v2|; // 1*4 + 2*5 + 3*6 = 32 all floats
@println => |"dot(v1, v2): " + dot_val|
use math;
let m1: arr = math.matrix => |[[1, 2], [3, 4]]|;
let m2: arr = math.matrix => |[[5, 6], [7, 8]]|;
@println => |"m1: " + m1|
@println => |"m2: " + m2|
use math;
let m1: arr = [[1.0, 2.0], [3.0, 4.0]];
let m2: arr = [[5.0], [6.0]];
let out: arr = math.matmul => |m1, m2|;

FS (File System) Library

All paths are string. Most functions throw a runtime error if the operation fails (missing file, permissions, invalid path, etc.).

Functions:

use fs;
let file_path: string = "data.txt";

let data: string = fs.read_file => |file_path|;
@println => |"File data: " + data|
use fs;
let file_path: string = "data.txt";

fs.write_file => |file_path, "This is some text"|
use fs;
fs.append_file => |file_path, "\nThis is some more text."|
use fs;
let dir_entries: arr = fs.read_dir => |"tests"|;
@println => |"Read dir entries: " + dir_entries|
use fs;
let lines: arr = fs.read_lines => |file_path|;
@println => |"Read lines: " + lines|
use fs;
let dir_path: string = "test_dir";
fs.create_dir => |dir_path|
use fs;
let dir_path: string = "test_dir";
fs.remove_dir => |dir_path|
use fs;
let dir_path: string = "test_dir";
let dir_exists: bool = fs.exists => |dir_path|;
@println => |"Dir exists: " + dir_exists|
use fs;
let file_path: string = "data.txt";
let path_is_file: bool = fs.is_file => |file_path|;
@println => |"Is file: " + path_is_file|
use fs;
let dir_path: string = "test_dir";
let path_is_dir: bool = fs.is_dir => |dir_path|;
@println => |"Is dir: " + path_is_dir|
use fs;
let file_path: string = "data.txt";
fs.remove_file => |file_path|
use fs;
let file_path: string = "data.txt";
let copy_path: string = "data_copy.txt";
let copied_bytes: int = fs.copy_file => |file_path, copy_path|;
@println => |"Bytes copied: " + copied_bytes|
use fs;
let copy_path: string = "data_copy.txt";
let moved_path: string = "data_moved.txt";
fs.rename => |copy_path, moved_path|
use fs;
let file_path: string = "data.txt";
let stats: obj = fs.stat => |file_path|;

OS (Operating System) Library

These functions expose OS/process info. Some features can be disabled at runtime (for example, command execution).

Functions:

use os;
let cwd: string = os.cwd => ||;
@println => |"Current directory: " + cwd|
use os;
let files: arr = os.ls => |"./images"|;
@println => |"Files: " + files|
use os;
let env_val: string = os.env => |"ZK_TEST_VAR"|;
@println => |"ZK_TEST_VAR: " + env_val|
use os;
os.set_env => |"ZK_TEST_VAR", "hello"|
use os;
os.remove_env => |"ZK_TEST_VAR"|
use os;
let plat: string = os.platform => ||;
@println => |"Platform: " + plat|
use os;
let args: arr = os.args => ||;
@println => |"Args: " + args|
use os;
let home: string = os.home_dir => ||;
@println => |"Home directory: " + home|
use os;
let temp: string = os.temp_dir => ||;
@println => |"Temp directory: " + temp|
use os;
let host: string = os.hostname => ||;
@println => |"Hostname: " + host|
use os;
let user: string = os.username => ||;
@println => |"Username: " + user|
use os;
let arch: string = os.arch => ||;
@println => |"Arch: " + arch|
use os;
let cpu_count: int = os.cpu_count => ||;
@println => |"CPU Count: " + cpu_count|
use os;
let uptime: int = os.uptime_ms => ||;
@println => |"Uptime (ms): " + uptime|
use os;
let which_echo: string = os.which => |"echo"|;
@println => |"which echo: " + which_echo|
use os;
os.exit => |0|
use os;
let pid: int = os.pid => ||;
@println => |"Process ID: " + pid|
use os;
@println => |"Sleeping for 2 seconds..."|
os.sleep => |2000| // Sleep for 2 seconds
@println => |"Slept for 2 seconds"|
use os;
let exec_result: obj = os.exec => |"echo", ["Hello, World!"]|;
@println => |"Exec status: " + exec_result.status|
@println => |"Exec stdout: " + exec_result.stdout|
@println => |"Exec stderr: " + exec_result.stderr|
use os;
let system_status: int = os.system => |"echo", ["Hello, World!"]|;
@println => |"system.status: " + system_status|
use os;
let spawn_pid: int = os.spawn => |"sleep", ["0.1"]|;
@println => |"spawn.pid: " + spawn_pid|

Encoding Library

Encoding helpers operate on string input and return string output. Decode functions throw a runtime error on invalid input.

Functions:

use encoding;
let out: string = encoding.base64_encode => |"Hello, Zekken!"|;
@println => |out|
use encoding;
let decoded: string = encoding.base64_decode => |"SGVsbG8sIFpla2tlbiE="|;
@println => |decoded|
use encoding;
let out: string = encoding.hex_encode => |"ABC123"|;
@println => |out|
use encoding;
let decoded: string = encoding.hex_decode => |"414243313233"|;
@println => |decoded|
use encoding;
let out: string = encoding.url_encode => |"hello world?x=1&y=2"|;
@println => |out|
use encoding;
let decoded: string = encoding.url_decode => |"hello%20world%3Fx%3D1%26y%3D2"|;
@println => |decoded|

Path Library

Cross-platform path helpers. All inputs and outputs are string.

Functions:

use path;
let p: string = path.join => |"tests", "libraries", "os.zk"|;
@println => |p|
use path;
let out: string = path.normalize => |"a/./b/../c"|;
@println => |out|
use path;
let out: string = path.resolve => |"tests", ".."|;
@println => |out|
use path;
let out: string = path.basename => |"tests/libraries/os.zk"|;
@println => |out|
use path;
let out: string = path.dirname => |"tests/libraries/os.zk"|;
@println => |out|
use path;
let out: string = path.extname => |"tests/libraries/os.zk"|;
@println => |out|
use path;
let out: string = path.stem => |"tests/libraries/os.zk"|;
@println => |out|
use path;
let out: bool = path.is_abs => |"/tmp"|;
@println => |out|
use path;
let out: string = path.relative => |"tests", "tests/libraries/os.zk"|;
@println => |out|

HTTP Library

HTTP client and server helpers. On WASM builds, HTTP is disabled. On native builds, you can disable it with ZEKKEN_DISABLE_HTTP=1.

Client

use http;
// Native runtime only (HTTP is disabled in WASM builds).
let resp: obj = http.request => |"GET", "https://example.com"|;
@println => |resp.status|
use http;
// Native runtime only.
let resp: obj = http.get => |"https://example.com"|;
@println => |resp.ok|
use http;
// Native runtime only.
let resp: obj = http.post => |"https://example.com", "hello"|;
@println => |resp.status|
use http;
// Native runtime only.
let data: any = http.get_json => |"https://example.com/data.json"|;
@println => |data|
use http;
let qs: string = http.build_query => |{ a: 1, b: "two" }|;
@println => |qs|
use http;
let obj: obj = http.parse_query => |"a=1&b=two"|;
@println => |obj|

Server

use http;
// Native runtime only.
let html: string = "<!doctype html><h1>Zekken HTTP</h1>";
let routes: obj = {
  "/": { status: 200, headers: { "content-type": "text/html; charset=utf-8" }, body: html }
};
http.serve => |"127.0.0.1:8080", routes|
use http;
// Native runtime only.
let server: obj = http.listen => |"127.0.0.1:8080"|;
@println => |"listening on: " + (server.addr => ||)|