Elder by example
Runnable programs for the interpreter, from a first functor to an HTTP endpoint.
Hello, Elder
Define a typed functor with a default and call it with a named argument. Save as hello.ldr and run elder hello.ldr.
greet::(name: string = "world") {
return "Hello, {name}!"
}
print(greet())
print(greet(name = "Elder"))
Summarize a list
Iterate through a typed list and read three named outputs. elder report.ldr prints count=3, total=18, mean=6.
report::(items: list(number)) {
count: int := items/length
total: number := 0
for (items) [item] {
total = total + item
}
mean := if (count > 0) { total / count } else { null }
}
result := report([4, 6, 8])
print("count={result/count}, total={result/total}, mean={result/mean}")
Decode data into a named type
Reject duplicate keys during decoding, then check the decoded value against the named Job type before accessing its fields. Run elder decode.ldr.
Job::(job: string, attempts: int)
payload: string := '[job = "render", attempts = 2]'
job: Job :=
eval(payload, execute = false, duplicate_keys = "reject")
print("{job/job}: {job/attempts} attempts")
Read command-line arguments
Run elder arguments.ldr -- --limit 3 to see both the typed option and the raw argument tokens. Passing --limit invalid exits with a usage error.
fetch(limit: number = 10)
print("limit={limit}")
print(repr(cli("argv")))
Join parallel tasks
Start three tasks and collect results in spawn order. Run elder parallel.ldr to print 4, 9, and 16.
square::(value: number) {
return value * value
}
values: list(int) := [2, 3, 4]
group := task_group(timeout_ms = 5000, abort_on_timeout = true)
for (values) [value] {
task_group/spawn(group, square, value)
}
results := task_group/join_all(group)
task_group/destroy_all(group)
for (results) [result] {
if (!result/ok) {
raise("error", "square task did not complete")
}
print(int(result/value))
}
Store a counter across runs
Run elder counter.ldr twice in the same directory. The second invocation reads the stored revision and increments the count. Start in a fresh directory to see counts 1 and 2.
db := fs_open("./counter_state")
current := store/get(db, collection = "counters", key = "visits")
count := 1
if (current == null) {
store/set(db, collection = "counters", key = "visits", value = count)
} else {
count = current/value + 1
store/set(db, collection = "counters", key = "visits",
value = count, base_revision = current/revision)
}
print("visit number {count}")
Use Elder in shell mode
Run elder shell.ldr on a Unix-like system. The condition is an Elder expression, then the script invokes the external program by its absolute path.
#!shell
n := 3
if (n > 2) {
/bin/echo "the bound name n is greater than two"
}
Serve a health endpoint
With the elder/web package installed, run elder http.ldr and request http://127.0.0.1:18085/health. It returns HTTP 200 and the body ok.
web := import("elder/web")
Reply::(code: int, body: string, mime: string = "text/plain")
health::(request, body = []) {
path: string := get(request, "full_path", "")
method: string := get(request, "method", "GET")
if (path == "/health" and string/to_lower(method) == "get") {
return Reply(code = 200, body = "ok")
}
return Reply(code = 404, body = "not found")
}
io/apie/serve(host = "127.0.0.1", port = 18085, workers = 2,
preprocessor = health, preprocessor_terminal = true)
Keep exploring
The language reference explains the contract behind these programs. The Elder source checkout's test/feature and test/fixtures/cli_conformance directories contain additional examples and conformance cases.