Functions
Named function (block body)
Section titled “Named function (block body)”fn greet name console.log[name]Compiles to:
function greet(name) { console.log(name);}Named function (expression body)
Section titled “Named function (expression body)”The to form creates a one-liner function body. Named functions do not have implicit return — use to return for explicit return.
fn greet name to console.log[name]function greet(name) { console.log(name); }Explicit return with to return
Section titled “Explicit return with to return”fn double x to return x mul 2function double(x) { return x * 2; }Function with no arguments
Section titled “Function with no arguments”Simply omit the parameters:
fn say-hello console.log[///Hello!///]function say_hello() { console.log("Hello!");}With expression body:
fn say-hello to console.log[///Hello!///]function say_hello() { console.log("Hello!"); }Multiple parameters
Section titled “Multiple parameters”Use ; to separate parameters:
fn add a; b return a add bfunction add(a, b) { return a + b;}Anonymous functions
Section titled “Anonymous functions”const double be fn x to x mul 2const double = (x) => x * 2;Anonymous function with no arguments
Section titled “Anonymous function with no arguments”const get-time be fn to Date.now[]const get_time = () => Date.now();Anonymous function with block body
Section titled “Anonymous function with block body”const process be fn data console.log[data] return dataconst process = (data) => { console.log(data); return data;};Async functions
Section titled “Async functions”async fn fetch-data url const res be await fetch[url] return resasync function fetch_data(url) { const res = await fetch(url); return res;}Async function expressions
Section titled “Async function expressions”Async anonymous functions work like regular anonymous functions with async prefix:
const handler be async fn event to await process[event]
const fetcher be async fn url const res be await fetch[url] return await res.json[]const handler = async (event) => await process(event);
const fetcher = async (url) => { const res = await fetch(url); return await res.json();};Inline Callbacks
Section titled “Inline Callbacks”With multi-line bracket support, anonymous functions can be passed as arguments in method chains:
promise.then[fn result to console.log[result]].catch[fn err to console.error[err]]Multi-line form:
fetch[url].then[ fn response return response.json[]].then[ fn data console.log[data]].catch[ fn err console.error[err]]fetch(url).then((response) => { return response.json();}).then((data) => { console.log(data);}).catch((err) => { console.error(err);});Function calls
Section titled “Function calls”Use [] instead of ():
greet[///world///]add[1; 2]console.log[///hello///]greet("world");add(1, 2);console.log("hello");Nested function calls
Section titled “Nested function calls”In JavaScript, nested calls like a(b(c), d) use (). In Purus, since [] is used for both arrays and calls, use ; to separate arguments:
-- JS: a(b(c), d)a[b[c]; d]
-- JS: a(b, c(d, e))a[b; c[d; e]]
-- JS: console.log(Math.max(1, 2))console.log[Math.max[1; 2]]
-- JS: fn(a(1, 2), b(3, 4), c)fn[a[1; 2]; b[3; 4]; c]
-- JS: outer(inner1(x), inner2(y, z))outer[inner1[x]; inner2[y; z]]a(b(c), d);a(b, c(d, e));console.log(Math.max(1, 2));fn(a(1, 2), b(3, 4), c);outer(inner1(x), inner2(y, z));Type annotations (erased)
Section titled “Type annotations (erased)”fn add a of Number; b of Number gives Number to a add bType annotations with of and gives are erased in the JavaScript output.
Return
Section titled “Return”fn get-value return 42