erg/doc/JA/syntax/05_builtin_funcs.md
GreasySlug b30d1adf63 Doc: checked again the style of writing
and the parts not translated into Ja
2022-09-20 21:22:02 +09:00

1.6 KiB
Raw Blame History

組み込み関数

badge

if

ifは条件に応じて処理を変える関数です。

result: Option Int = if! Bool.sample!(), do:
    log "True was chosen"
    1
print! result # None (または1)

.sample!()は集合の値をランダムに返します。もし戻り値が真ならば、print! "True"が実行されます。 条件が偽であった際の処理も指定できます。つ目のdoブロックはelseブロックと呼ばれます。

result: Nat = if Bool.sample!():
    do:
        log "True was chosen"
        1
    do:
        log "False was chosen"
        0
print! result # 1 (または0)

処理が1行ならば、インデントを省略できます。

result = if Bool.sample!():
    do 1
    do 0

for

繰り返し行う処理を書くときはforが使えます。

match_s(ss: Iterator(Str), pat: Pattern): Option Str =
    for ss, s ->
        if pat.match(s).is_some():
            break s

Previous | Next