関数
名前付き関数(ブロック本体)
Section titled “名前付き関数(ブロック本体)”fn greet name console.log[name]コンパイル結果:
function greet(name) { console.log(name);}名前付き関数(式本体)
Section titled “名前付き関数(式本体)”to 形式はワンライナーの関数本体を作成します。名前付き関数には暗黙のreturnはありません — 明示的にreturnするには to return を使用してください。
fn greet name to console.log[name]function greet(name) { console.log(name); }to return による明示的なreturn
Section titled “to return による明示的なreturn”fn double x to return x mul 2function double(x) { return x * 2; }引数なしの関数
Section titled “引数なしの関数”パラメータを省略するだけです:
fn say-hello console.log[//;Hello!;//]function say_hello() { console.log("Hello!");}式本体の場合:
fn say-hello to console.log[//;Hello!;//]function say_hello() { console.log("Hello!"); }複数パラメータ
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;引数なしの無名関数
Section titled “引数なしの無名関数”const get-time be fn to Date.now[]const get_time = () => Date.now();ブロック本体の無名関数
Section titled “ブロック本体の無名関数”const process be fn data console.log[data] return dataconst process = (data) => { console.log(data); return data;};async fn fetch-data url const res be await fetch[url] return resasync function fetch_data(url) { const res = await fetch(url); return res;}非同期関数式
Section titled “非同期関数式”非同期無名関数は通常の無名関数と同様に async プレフィックスを付けます:
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();};インラインコールバック
Section titled “インラインコールバック”複数行括弧のサポートにより、無名関数をメソッドチェーンの引数として渡せます:
promise.then[fn result to console.log[result]].catch[fn err to console.error[err]]複数行形式:
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);});関数呼び出し
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) のようなネストされた呼び出しでは、[] が配列と呼び出しの両方に使われるため、; で引数を区切ります:
-- JS: a(b(c), d)a[b[c]; d]
-- JS: a(b, c(d, e))a[b; c[d; e]]
-- JS: console.log(m.max(1, 2))use p-math as mconsole.log[m.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));const m = { max: Math.max };console.log(m.max(1, 2));fn(a(1, 2), b(3, 4), c);outer(inner1(x), inner2(y, z));無視するパラメータ(blank)
Section titled “無視するパラメータ(blank)”blank を使うと、そのパラメータ位置を明示的に無視できます。Array.from の最初の引数(要素)が不要なコールバックなどで便利です。
-- 最初の引数を無視し、インデックスのみ使用const indices be Array.from[[length be 5]; fn blank; i to i]const indices = Array.from({ length: 5 }, (_, i) => i);複数の blank パラメータはstrictモードエラーを避けるため、JSでは _・_1・_2… のように連番になります:
fn f blank; blank; x to xfunction f(_, _1, x) { return x; }switch のcatch-allアームとしても使用できます:
switch status case //;ok;// then //;good;// case blank then //;unknown;//if (status === "ok") { "good";} else { "unknown";}型アノテーション(消去される)
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ジェネレータ関数
Section titled “ジェネレータ関数”yield を含む関数は自動的にジェネレータ(function*)としてコンパイルされます。特別な構文は不要で、関数本体内で yield を使用するだけです:
fn count-up limit let i be 0 while i lt limit yield i i add be 1function* countUp(limit) { let i = 0; while (i < limit) { yield i; i += 1; }}値なしのyield
Section titled “値なしのyield”fn signals yield console.log[//;resumed;//] yieldfunction* signals() { yield; console.log("resumed"); yield;}ジェネレータの使用
Section titled “ジェネレータの使用”const gen be count-up[5]for val in gen console.log[val]const gen = countUp(5);for (const val of gen) { console.log(val);}