Built-in Functions Reference

Complete reference for all built-in functions in Keikaku. Click on any function to see its usage.

Input/Output

declare(...args)

Print values to standard output, space-separated

declare("Hello", name, 42)
inquire(prompt)

Read user input from stdin, returns string

name := inquire("Enter name: ")

Type Conversion

text(value)

Convert any value to its string representation

text(42) # "42"
number(value)

Convert string to integer

number("42") # 42
decimal(value)

Convert string to float

decimal("3.14") # 3.14
classify(value)

Get type name as string

classify([1,2]) # "list"
boolean(value)

Convert to boolean

boolean(1) # true

Collections (List/Dict)

measure(x)

Get length of list, string, or dict

measure([1,2,3]) # 3
push(list, item)

Append item to list (mutates)

push(items, "new")
pop(list)

Remove and return last item

last := pop(items)
reverse(list)

Return reversed copy of list

reverse([1,2,3]) # [3,2,1]
sort(list)

Return sorted copy of list

sort([3,1,2]) # [1,2,3]
span(n)

Generate list [0, 1, ..., n-1]

span(5) # [0,1,2,3,4]
span(start, end)

Generate list [start, ..., end-1]

span(2, 6) # [2,3,4,5]
slice(list, start, end)

Extract sublist

slice([1,2,3,4], 1, 3) # [2,3]
contains(list, item)

Check if list contains item

contains([1,2,3], 2) # true
index(list, item)

Find index of item (-1 if not found)

index([1,2,3], 2) # 1
keys(dict)

Get list of dictionary keys

keys({"a": 1}) # ["a"]
values(dict)

Get list of dictionary values

values({"a": 1}) # [1]

String Manipulation

uppercase(s)

Convert string to uppercase

uppercase("hello") # "HELLO"
lowercase(s)

Convert string to lowercase

lowercase("HELLO") # "hello"
split(s, delimiter)

Split string into list

split("a,b,c", ",") # ["a","b","c"]
join(list, delimiter)

Join list into string

join(["a","b"], "-") # "a-b"
trim(s)

Remove leading/trailing whitespace

trim(" hi ") # "hi"
startswith(s, prefix)

Check if string starts with prefix

startswith("hello", "he") # true
endswith(s, suffix)

Check if string ends with suffix

endswith("hello", "lo") # true
replace(s, old, new)

Replace occurrences in string

replace("hello", "l", "L") # "heLLo"
substr(s, start, length)

Extract substring

substr("hello", 1, 3) # "ell"

Math Functions

abs(x)

Absolute value

abs(-5) # 5
min(a, b)

Return smaller value

min(3, 7) # 3
max(a, b)

Return larger value

max(3, 7) # 7
floor(x)

Round down to integer

floor(3.7) # 3
ceil(x)

Round up to integer

ceil(3.2) # 4
round(x)

Round to nearest integer

round(3.5) # 4
sqrt(x)

Square root

sqrt(16) # 4.0
power(base, exp)

Exponentiation

power(2, 8) # 256
random(min, max)

Random integer in range [min, max]

random(1, 100) # 42
sin(x)

Sine (radians)

sin(3.14159 / 2) # ~1.0
cos(x)

Cosine (radians)

cos(0) # 1.0
log(x)

Natural logarithm

log(2.718) # ~1.0

File Operations

decipher(filepath)

Read entire file as string

content := decipher("data.txt")
inscribe(filepath, content)

Write string to file

inscribe("out.txt", "Hello")
append_file(filepath, content)

Append to file

append_file("log.txt", "Entry\n")
file_exists(filepath)

Check if file exists

file_exists("config.json") # true/false
delete_file(filepath)

Delete a file

delete_file("temp.txt")
list_dir(path)

List directory contents

list_dir("./src") # ["a.kei", "b.kei"]

Generator Control

proceed(gen)

Advance generator and get next value

value := proceed(my_gen)
transmit(gen, value)

Send value into generator, get next yield

result := transmit(gen, 42)
receive()

Inside generator: get transmitted value

received := receive()
disrupt(gen, error)

Throw exception into generator

disrupt(gen, "Timeout")

Async & Timing

sleep(ms)

Pause execution for milliseconds

sleep(1000) # Wait 1 second
defer(ms, protocol, ...args)

Schedule delayed execution

defer(500, notify, "Done!")
resolve(value)

Create immediately resolved promise

p := resolve(42)
timestamp()

Get current Unix timestamp (seconds)

now := timestamp()
timestamp_ms()

Get current Unix timestamp (milliseconds)

now_ms := timestamp_ms()

JSON Processing

encode_json(value)

Convert value to JSON string

encode_json({"a": 1}) # '{"a":1}'
decode_json(string)

Parse JSON string to value

decode_json('{"a":1}') # {"a": 1}

Higher-Order Functions

transform(list, fn)

Map function over list

transform([1,2], protocol(x): yield x*2) # [2,4]
select(list, fn)

Filter list by predicate

select([1,2,3], protocol(x): yield x>1) # [2,3]
fold(list, fn, initial)

Reduce list to single value

fold([1,2,3], protocol(a,b): yield a+b, 0) # 6

Complete Examples

File Processing Pipeline

KEIKAKU Protocol
1# Read CSV, process, write output
2content := decipher("data.csv")
3lines := split(content, "\n")
4
5results := []
6cycle through lines as line:
7 foresee measure(trim(line)) > 0:
8 fields := split(line, ",")
9 processed := uppercase(fields[0])
10 push(results, processed)
11
12output := join(results, "\n")
13inscribe("output.txt", output)
14declare("Processed", measure(results), "lines")

Data Transformation

KEIKAKU Protocol
1# Transform, filter, reduce pattern
2numbers := span(1, 101)
3
4# Double all numbers
5doubled := transform(numbers, protocol(x): yield x * 2)
6
7# Keep only divisible by 10
8filtered := select(doubled, protocol(x): yield x % 10 == 0)
9
10# Sum them up
11total := fold(filtered, protocol(a, b): yield a + b, 0)
12
13declare("Sum of even multiples of 10:", total)