関数
名前付き関数(ブロック本体)
Section titled “名前付き関数(ブロック本体)”fn greet name console.log[name]コンパイル結果:
function greet(name) { console.log(name);}名前付き関数(式本体)
Section titled “名前付き関数(式本体)”fn double x to x mul 2function double(x) { return x * 2;}複数パラメータ
Section titled “複数パラメータ”パラメータの区切りには ; を使います:
fn add a; b return a add bfunction add(a, b) { return a + b;}const double be fn x to x mul 2const double = (x) => x * 2;async fn fetch-data url const res be await fetch[url] return resasync function fetchData(url) { const res = await fetch(url); return res;}関数呼び出し
Section titled “関数呼び出し”() の代わりに [] を使います:
greet[///world///]add[1; 2]console.log[///hello///]greet("world");add(1, 2);console.log("hello");ネストされた関数呼び出し
Section titled “ネストされた関数呼び出し”JavaScriptでは a(b(c), d) のようにネストした呼び出しには () を使います。Purusでは [] を配列と呼び出しの両方に使うため、引数の区切りに ; を使います:
-- 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));型注釈(消去される)
Section titled “型注釈(消去される)”fn add a of Number; b of Number gives Number to a add bof と gives による型注釈はJavaScript出力では消去されます。
return
Section titled “return”fn get-value return 42