本文へ移動
Hikari リファレンス

prelude リファレンス

prelude は起動時に root フレームへ束縛される組込群 (関数・型名・namespace object)。import なしでどこからでも使える。

Any

Any

任意の値に一致するトップ型。

>>> 1 matches Any
true

AtMostOnce

AtMostOnce(T)

多重度型。高々 1 回まで使える値の器 (アフィン)。使い残しは咎めない。
値の構造は T のまま — 取り出しは要らない。実行時照合は基底型 T に落ちる。

>>> 1 matches AtMostOnce(Int)
true

Bool

Bool

真偽値の原始型。

>>> true matches Bool
true

Borrowed

Borrowed(T)

多重度型の値を借用した位置の型。消費できない。実行時照合は基底型 T に落ちる。

>>> 1 matches Borrowed(Int)
true

Broke

Broke(v)
Broke: { Any | Control }

ブロックが break(v) したことを表す Control。

>>> Broke(1)
Broke(1)

Bytes

Bytes

バイト列の原始型。値は bytes(...) や std:fs の read などが作る。

>>> bytes([104, 105]) matches Option(Bytes)
true

Comparable

Comparable

順序を持つ値の構造的 interface。原始型または callable な compare スロットを
持つ object に一致する。

>>> 1 matches Comparable
true

Consuming

Consuming(F)

レシーバーを消費するメソッドのマーク。F は関数型。多重度型のスロットにのみ書ける。

>>> type H := AtMostOnce({ close: Consuming({| Unit }) |})
>>> H
AtMostOnce({ close: Consuming({| Unit }) |})

Continued

Continued
Continued: Control

ブロックが continue したことを表す Control (payload 無し)。

>>> Continued
Continued

Control

Control

capture が返す制御結果 (Normal / Broke / Continued / Returned) の型。

>>> capture { 1 } matches Control
true

Copyable

Copyable

複製できる値の構造的 interface。Future や複製を定義できない不透明リソース
以外のほぼ全値に一致する。

>>> [1, 2] matches Copyable
true

Effect

Effect(T, …)

効果型。結果型 T と、その計算が起こす効果ラベルの集合を述べる。関数型の結果型位置に
だけ書ける。値の構造は T のままで取り出しは要らず、実行時照合は基底型 T に落ちる。
効果ゼロの Effect(T) は T に退化する。

>>> 1 matches Effect(Int, Io)
true

EffectConduit

EffectConduit(F)

そのパラメーターの効果が呼び出し元へ透過することを表す位置マーカー。F は関数型。
純粋なブロックを渡せば呼び出しも純粋になる。実行時照合は基底型 F に落ちる。

>>> { x | x } matches EffectConduit({ Int | Int })
true

Equal

Equal
Equal: Ordering

順序比較の結果 (等しい) を表す Ordering。

>>> Equal
Equal

Err

Err(e)
Err: { b | Result(a, b) }

失敗 (エラー e、既定は標準 Error) を表す Result コンストラクター。

>>> Err(1)
Err(1)

Error

Error

回復可能エラー値の shape エイリアス (= kind と message を持つ record)。
Result の既定エラー型。

>>> { kind := "io", message := "boom" |} matches Error
true

ExactlyOnce

ExactlyOnce(T)

多重度型。ちょうど 1 回使う値の器 (線形)。未消費は診断の対象になる。
実行時照合は基底型 T に落ちる。

>>> 1 matches ExactlyOnce(Int)
true

Float

Float

浮動小数点数の原始型。

>>> 1.5 matches Float
true

Fs

Fs

効果ラベル。ファイルシステム。

>>> Fs

Future

Future(T)

並行実行の結果値 (fork が返す) の型コンストラクター。

>>> Future(Int)
Future(Int)

Greater

Greater
Greater: Ordering

順序比較の結果 (前者が大きい) を表す Ordering。

>>> Greater
Greater

Int

Int

整数の原始型。

>>> 3 matches Int
true

Io

Io

効果ラベル。対話デバイスへの入出力 (端末)。

>>> Io

Less

Less
Less: Ordering

順序比較の結果 (前者が小さい) を表す Ordering。

>>> Less
Less

List

List(T)

リストの型コンストラクター。List(T) は全要素が T のリストに一致する (空リストも一致)。

>>> [1, 2] matches List(Int)
true

Mut

Mut

効果ラベル。自分の呼び出しフレームの外にある可変状態への書き込み。

>>> Mut

Net

Net

効果ラベル。ネットワーク。

>>> Net

Never

Never

値を持たないボトム型。panic / exit など発散位置の結果型で、注釈に書くと
「決して返らない」を表明できる。どの値にも一致しない。

>>> 1 matches Never
false

NonZero

NonZero

0 でない Int を表す refinement 型。/% の除数にかかる非ゼロの証明義務を
書き手が引き受けるための綴りである。

>>> 5 matches NonZero
true
>>> 0 matches NonZero
false

NonZeroFloat

NonZeroFloat

0.0 でも -0.0 でもない Float を表す refinement 型。-0.0 での除算も実行時エラーで、
全順序では 0.0 と別の値なので両方を除く。

>>> 2.5 matches NonZeroFloat
true
>>> (-0.0) matches NonZeroFloat
false

None

None
None: Option(Never)

値が不在であることを表す Option。

>>> None
None

Normal

Normal(v)
Normal: { Any | Control }

ブロックが正常終了し値 v を返したことを表す Control。

>>> Normal(1)
Normal(1)

Number

Number

数値グループ (= OneOf(Int, Float) の透過的エイリアス)。Int または Float に
一致する閉じた union。境界付き型変数の境界 (x: (a: Number)) に使うと
「複数引数が同じ数値型」まで表明できる (単独の注釈は同型を要求しない)。

>>> 3 matches Number
true

Ok

Ok(v)
Ok: { a | Result(a, b) }

成功 (値 v) を表す Result コンストラクター。

>>> Ok(1)
Ok(1)

OneOf

OneOf(A, B, ...)

直和の型コンストラクター (引数は型 2 個以上)。いずれかの型に一致すれば一致する。

>>> 3 matches OneOf(Int, Bool)
true

Option

Option(T)

有無を表す型コンストラクター (= Some(T) または None)。

>>> Some(1) matches Option(Int)
true

Ordering

Ordering

順序比較の結果 (Less / Equal / Greater) の型。compare が返す。

>>> 1.compare(2) matches Ordering
true

Proc

Proc

効果ラベル。プロセス環境。

>>> Proc

Rand

Rand

効果ラベル。非決定。

>>> Rand

Range

Range

range(...) が作る遅延シーケンス値の型。

>>> range(0, 3) matches Range
true

Result

Result(T, E)

成否を表す型コンストラクター (= Ok(T) または Err(E))。Result(T) はエラー型を
既定の Error にした Result(T, Error) と同じ。

>>> Ok(1) matches Result(Int)
true

Returned

Returned(v)
Returned: { Any | Control }

ブロックが return(v) したことを表す Control。

>>> Returned(1)
Returned(1)

Sequence

Sequence

要素の並びを持つ値の構造的 interface。List / String / Bytes / range に一致する
(タプルは length しか持たないので一致しない)。

>>> [1, 2] matches Sequence
true

Some

Some(v)
Some: { Any | Option(Any) }

値 v があることを表す Option コンストラクター。

>>> Some(1)
Some(1)

String

String

文字列の原始型。

>>> "a" matches String
true

Susp

Susp

効果ラベル。中断点 (未完了 Future への !・sleep・wait_any)。

>>> Susp

Time

Time

効果ラベル。時計を読む。

>>> Time

Type

Type(T)

型値の型コンストラクター。Type(T) は「型 T を表す型値」に一致する。
型を引数にとる関数がその意図をシグネチャに書くために使う。

>>> Int matches Type(Int)
true

Unit

Unit

空タプル () の型。「値が無い」ことを表す。

>>> () matches Unit
true

all

all(futures)
all: { List(Future(Any)) | Future(List(Any)) }

全 Future の解決を待ち、値の List を入力順で運ぶ Future を返す。

>>> print all([fork { 1 }, fork { 2 }])!
[1, 2]

break

break! / break(value)
break: { Any | Unit }

囲うループを抜ける (値つき・多値可)。

>>> while {true} {break(42)}
42

bytes

bytes(list)
bytes: { List(Int) | Option(Bytes) }

Int の List から Bytes を構築する (Option(Bytes))。

>>> bytes([65, 66])
Some(<2 bytes>)

capture

capture(block, args…)
capture: { Any | Control }

block を境界でトラップせず起動し、制御転帰を Control 値で返す (loop の基底)。

>>> capture({42})
Normal(42)

clamp

clamp(x, lo, hi)
clamp: { (a: Comparable), a, a | a }

x を [lo, hi] に収める (3 引数は同じ Comparable 型)。lo > hi はエラー。

>>> clamp(10, 0, 5)
5

continue

continue!
continue: { Unit }

今の反復を打ち切り、次の反復へ進む。

>>> mutable i := 0
>>> while {i < 3} {i = i + 1; if (i == 2) {continue!} {()}; println(i)}
1
3

eprint

eprint(s)
eprint: { Any | Unit }

文字列 s を改行を付けずに標準エラー出力に書く (print の stderr 版)。

効果ハンドラーの操作である。handle の操作スロットに eprint と書いて横取りできる(language-spec.md §17.9)。

>>> eprint("err")

eprintln

eprintln(s)
eprintln: { Any | Unit }

文字列 s を改行付きで標準エラー出力に書く (println の stderr 版。診断・エラー用)。

効果ハンドラーの操作である。handle の操作スロットに eprintln と書いて横取りできる(language-spec.md §17.9)。

>>> eprintln("err")

error

error(kind, message)
error: { String, String | Error }

kind と message から標準 Error 値を構築する。

>>> error("io", "boom")
{ kind := "io", message := "boom" |}
>>> error("io", "boom") matches Error
true

exit

exit(code)
exit: { Int | Never }

プロセスを exit code code で即座に終了する。発散するため結果は Never。

効果ハンドラーの操作である。handle の操作スロットに exit と書いて横取りできる(language-spec.md §17.9)。

>>> exit(0)

fork

fork { body }
fork: { { Any } | Future(Any) }

body を別フローで評価開始し、Future を返す。必須スロットが未束縛の block は fork 自身が panic。

>>> f := fork { 1 + 2 }
>>> print f!
3

forward

forward(c)
forward: { Control | Any }

Control を直接形へ送り直す (break/continue/return を再送出)。conduit。

>>> r := { c := capture({ return(9) }); forward(c); 99 }
>>> r()
9

handle

handle { block } { op := { args…, k | … } |}
handle: { { Any }, Any | Any }

block を起動し、操作スロットに挙げた効果操作を横取りしてその操作スロットへ渡す。
操作スロットの第 2 引数が継続で、呼べば操作の地点から再開し、呼ばなければ脱出になる。

>>> r := handle {println("x"); 7} {println := {s, k | println("[" + s + "]"); k(())} |}
>>> println(r)
[x]
7

if

if cond { then } { else }
if: { Bool, EffectConduit({ Any }), EffectConduit({ Any }) | Any }

cond が真なら then、偽なら else を評価する (3 引数必須)。

>>> if true {1} {2}
1

input

input(prompt)
input: { String | Future(Result(String)) }

prompt を表示し、1 行入力を Future として返す。

効果ハンドラーの操作である。handle の操作スロットに input と書いて横取りできる(language-spec.md §17.9)。

>>> name := input("お名前: ")!
>>> print name

max

max(a, b)
max: { (a: Comparable), a | a }

a と b の大きい方 (両者は同じ Comparable 型)。同値は a を返す。

>>> max(3, 7)
7

min

min(a, b)
min: { (a: Comparable), a | a }

a と b の小さい方 (両者は同じ Comparable 型)。同値は a を返す。

>>> min(3, 7)
3

now

now!
now: { Int }

現在時刻を UNIX epoch からのミリ秒 (符号付き) で返す (中断しない)。

効果ハンドラーの操作である。handle の操作スロットに now と書いて横取りできる(language-spec.md §17.9)。

>>> print now!
1735689600000

panic

panic(message)
panic: { String | Never }

message を持つバグ層エラーを発火する (バグの表明)。発散するため結果は Never。

>>> panic("到達しないはず")

print

print(s)
print: { Any | Unit }

文字列 s を改行を付けずに標準出力に書く。カーソル制御と組み合わせた TUI の
部分描画など、行末に改行を入れたくない出力に使う (末尾改行が要るなら println)。

効果ハンドラーの操作である。handle の操作スロットに print と書いて横取りできる(language-spec.md §17.9)。

>>> print("hi")
hi

println

println(s)
println: { Any | Unit }

文字列 s を改行付きで標準出力に書く (print + 末尾改行)。行単位の通常出力に使う。

効果ハンドラーの操作である。handle の操作スロットに println と書いて横取りできる(language-spec.md §17.9)。

>>> println("hello")
hello

race

race(futures)
race: { List(Future(Any)) | Future(Any) }

最初に解決した Future の値を運ぶ Future を返す。

>>> print race([fork { sleep 5; 1 }, fork { 2 }])!
2

range

range(start, end)
range: { Int, Int | Range }

半開区間 [start, end) の整数列 (遅延シーケンス) を返す。

>>> range(1, 4).to_list()
[1, 2, 3]

return

return! / return(value)
return: { Any | Unit }

関数 (body) を抜けて値を返す (値つき・多値可)。

>>> f := {| return(5); 99 }
>>> f()
5

sleep

sleep(ms)
sleep: { Int | Unit }

整数 ms ミリ秒だけ現フローを中断する。

効果ハンドラーの操作である。handle の操作スロットに sleep と書いて横取りできる(language-spec.md §17.9)。

>>> print (sleep 0)
()

spawn

spawn { body }
spawn: { { Any } | Future(Any) }

body を隔離した専用フローで並列評価し、Future を返す。捕獲した可変状態は複製して
切り離す (子の変異は親から見えない)。非 Copyable を捕獲していると spawn 自身が panic。
必須スロット未束縛の block も spawn 自身が panic。

>>> f := spawn { 1 + 2 }
>>> print f!
3

wait_any

wait_any(futures)
wait_any: { List(Future(Any)) | Future(Any) }

Future の List を多重待ちし、最初に解決した Future を返す。

効果ハンドラーの操作である。handle の操作スロットに wait_any と書いて横取りできる(language-spec.md §17.9)。

>>> a := fork { sleep 5; 1 }
>>> b := fork { 2 }
>>> print wait_any([a, b])!
2

when

when cond { then }
when: { Bool, EffectConduit({ Any }) | Any }

cond が真なら then を評価する (else 省略版)。

>>> when true {print("yes")}
yes

while

while { cond } { body }
while: { EffectConduit({ Bool }), EffectConduit({ Any }) | Unit }

cond が真の間 body を繰り返す (条件もブロック)。

>>> mutable i := 0
>>> while {i < 3} {i = i + 1; println(i)}
1
2
3

Bool メソッド

not

b.not()
not: { Bool }

論理否定を返す。

>>> true.not()
false

Bytes メソッド

all

b.all { x | … }
all: { EffectConduit({ Int | Bool }) | Bool }

述語が全バイトで true なら true。空は true。

>>> "ab".to_bytes().all {b | b > 90}
true

any

b.any { x | … }
any: { EffectConduit({ Int | Bool }) | Bool }

述語が true を返すバイトが 1 つでもあれば true。空は false。

>>> "ab".to_bytes().any {b | b > 97}
true

contains

b.contains(sub)
contains: { Bytes | Bool }

sub (Bytes) が連続部分バイト列として現れれば true。

>>> "abc".to_bytes().contains("b".to_bytes())
true

count

b.count { x | … }
count: { EffectConduit({ Int | Bool }) | Int }

述語が true を返すバイトの個数を Int で返す。

>>> "ab".to_bytes().count {b | b > 97}
1

each

b.each(block)

各バイト (Int) に block を適用する。戻り値は ()。

>>> "ab".to_bytes().each {b | println(b)}
97
98

filter

b.filter(block)
filter: { EffectConduit({ Int | Bool }) | List(Int) }

block が true を返すバイトだけの新 List を返す。

>>> "ab".to_bytes().filter {b | b > 97}
[98]

find

b.find { x | … }
find: { EffectConduit({ Int | Bool }) | Option(Int) }

述語が最初に true を返すバイト (Int) を Some で返す。無ければ None。

>>> "ab".to_bytes().find {b | b > 97}
Some(98)

first

b.first()
first: { Option(Int) }

先頭の 1 バイトを Some(Int) で返す。空なら None。

>>> "ab".to_bytes().first()
Some(97)

fold

b.fold(init, block)
fold: { b, EffectConduit({ b, Int | b }) | b }

init から各バイト (Int) を (acc, 要素) で畳み込んだ単一値を返す。

>>> "ab".to_bytes().fold(0, {acc, b | acc + b})
195

has_position

b.has_position(i)
has_position: { Int | Bool }

i (Int) が 0 <= i < length なら true。

>>> "abc".to_bytes().has_position(1)
true

index_of

b.index_of(sub)
index_of: { Bytes | Option(Int) }

sub が最初に現れるバイト位置を Some(Int)。無ければ None。

>>> "abc".to_bytes().index_of("b".to_bytes())
Some(1)

is_empty

b.is_empty()
is_empty: { Bool }

length! == 0 と同値。バイトが無ければ true。

>>> "".to_bytes().is_empty()
true

last

b.last()
last: { Option(Int) }

末尾の 1 バイトを Some(Int) で返す。空なら None。

>>> "ab".to_bytes().last()
Some(98)

length

b.length()
length: { Int }

バイト数を返す。

>>> "ab".to_bytes().length()
2

map

b.map(block)
map: { EffectConduit({ Int | b }) | List(b) }

各バイトを block で変換した新 List を返す (Bytes ではない)。

>>> "ab".to_bytes().map {b | b + 1}
[98, 99]

positions

b.positions()
positions: { List(Int) }

位置インデックス [0, 1, …, length-1] を List(Int) で返す。

>>> "abc".to_bytes().positions()
[0, 1, 2]

slice

b.slice(start, end)
slice: { Int, Int | Bytes }

半開区間 [start, end) の部分 Bytes。範囲外・逆順はエラー。

>>> "hello".to_bytes().slice(1, 3)
<2 bytes>

to_list

b.to_list()
to_list: { List(Int) }

各バイト (Int 0〜255) を要素とする List(Int) に展開する。

>>> "ab".to_bytes().to_list()
[97, 98]

to_string

b.to_string()
to_string: { Option(String) }

UTF-8 デコードして Some(String)。不正 UTF-8 は None。

>>> "ab".to_bytes().to_string()
Some("ab")

Float メソッド

abs

f.abs()
abs: { Float }

絶対値を返す。

>>> (-3.5).abs()
3.5

ceil

f.ceil()
ceil: { Float }

f 以上の最小の整数値 (Float) を返す。

>>> 3.2.ceil()
4.0

floor

f.floor()
floor: { Float }

f 以下の最大の整数値 (Float) を返す。

>>> 3.2.floor()
3.0

is_finite

f.is_finite()
is_finite: { Bool }

f が有限値 (NaN でも Inf でもない) なら true。

>>> 1.5.is_finite()
true

is_infinite

f.is_infinite()
is_infinite: { Bool }

f が ±Inf なら true。

>>> (1.0e308 * 10.0).is_infinite()
true

is_nan

f.is_nan()
is_nan: { Bool }

f が NaN なら true。

>>> (-1.0).sqrt().is_nan()
true

round

f.round()
round: { Float }

四捨五入した整数値 (Float) を返す。

>>> 2.5.round()
3.0

sqrt

f.sqrt()
sqrt: { Float }

平方根を返す。

>>> 4.0.sqrt()
2.0

to_int

f.to_int()
to_int: { Option(Int) }

整数化する (0 方向への切り捨て)。NaN / Inf は None、それ以外は常に Some(Int)
(Int は任意精度なので int64 を超える有限値も正確に変換する)。

>>> 5.5.to_int()
Some(5)

trunc

f.trunc()
trunc: { Float }

小数部を切り捨てた整数値 (Float) を返す。

>>> 3.9.trunc()
3.0

Future メソッド

and_then

f.and_then { v | fut }
and_then: { EffectConduit({ a | Future(b) }) | Future(b) }

block が Future を返す flatMap。入れ子を平坦化。≡ fork { g(f!)! }。

>>> chained := (fork { 10 }).and_then {v | fork { v + 5 }}
>>> print chained!
15

cancel

f.cancel!
cancel: { Unit }

f を生んだフローのキャンセルを要求する。中断点で観測され Future は panic で解決。解決済みなら no-op。

>>> f := fork { 1 }
>>> v := f!
>>> print f.cancel!
()

is_ready

f.is_ready()
is_ready: { Bool }

f が解決済みなら true、未解決なら false。中断しない (! のように resolve を待たない)。

>>> f := fork { 1 + 2 }
>>> println f.is_ready()
>>> v := f!
>>> println f.is_ready()
>>> println v
false
true
3

map

f.map { v | … }
map: { EffectConduit({ a | b }) | Future(b) }

f の解決値に block を適用した新 Future。≡ fork { g(f!) }。f が panic していれば伝播。

>>> doubled := (fork { 21 }).map {v | v * 2}
>>> print doubled!
42

timeout

f.timeout(ms)
timeout: { Int | Future(Option(a)) }

ms 内に解決すれば Some(v)、超過すれば None を運ぶ Future(Option)。f は自動停止しない。

>>> fast := fork { 42 }
>>> print fast.timeout(200)!
Some(42)

Int メソッド

to_char

n.to_char()
to_char: { String }

Unicode code point (Int) を 1 文字 String に変換する。

>>> 65.to_char()
"A"

to_float

n.to_float()
to_float: { Float }

Float 化する全域関数。float64 で表現可能なら最近接丸めした Float、
範囲超 (約 1.8e308 超) は符号付き Inf を返す (Float 算術の overflow→Inf と同じ)。

>>> 5.to_float()
5.0

List メソッド

all

xs.all { x | … }
all: { EffectConduit({ a | Bool }) | Bool }

述語が全要素で true なら true。空は true。

>>> [1, 2, 3].all {x | x > 0}
true

any

xs.any { x | … }
any: { EffectConduit({ a | Bool }) | Bool }

述語が true を返す要素が 1 つでもあれば true。空は false。

>>> [1, 2, 3].any {x | x > 2}
true

contains

xs.contains(x)
contains: { a | Bool }

x と == で一致する要素があれば true。空 List は常に false。

>>> ["a", "b"].contains("a")
true

count

xs.count { x | … }
count: { EffectConduit({ a | Bool }) | Int }

述語が true を返す要素の個数を Int で返す。

>>> [1, 2, 3].count {x | x > 1}
2

drop

xs.drop(n)
drop: { Int | List(a) }

先頭 n 要素を除いた残りの新 List を返す (非破壊)。n は [0, length] にクランプ。

>>> [1, 2, 3].drop(1)
[2, 3]

drop_while

xs.drop_while { x | … }
drop_while: { EffectConduit({ a | Bool }) | List(a) }

先頭から述語が true の間を読み飛ばし、残り全部の新 List を返す。

>>> [1, 2, 3, 1].drop_while {x | x < 3}
[3, 1]

each

xs.each(block)

各要素に block を適用する。戻り値は ()。

>>> [1, 2, 3].each {x | println(x)}
1
2
3

filter

xs.filter(block)
filter: { EffectConduit({ a | Bool }) | List(a) }

block が true を返す要素だけの新リストを返す。

>>> [1, 2, 3].filter {x | x > 1}
[2, 3]

find

xs.find { x | … }
find: { EffectConduit({ a | Bool }) | Option(a) }

述語が最初に true を返す要素を Some(要素) で返す。無ければ None。

>>> [1, 2, 3].find {x | x > 1}
Some(2)

first

xs.first()
first: { Option(a) }

先頭要素を Some(T) で返す。空なら None。

>>> [1, 2, 3].first()
Some(1)

flat_map

xs.flat_map { x | … }
flat_map: { EffectConduit({ a | List(b) }) | List(b) }

各要素に block (List を返す) を適用し 1 段平坦化して連結した新 List を返す。

>>> [1, 2, 3].flat_map {x | [x, x]}
[1, 1, 2, 2, 3, 3]

flatten

xs.flatten()

各要素 (すべて List) を 1 段平坦化して連結した新 List を返す。非 List 要素はエラー。

>>> [[1], [2]].flatten()
[1, 2]

fold

xs.fold(init, block)
fold: { b, EffectConduit({ b, a | b }) | b }

init からの左畳み込み。各要素で block を (acc, 要素) で呼び、戻り値を次の acc にする。

>>> [1, 2, 3].fold(0, {acc, x | acc + x})
6

has_name

obj.has_name(k)
has_name: { String | Bool }

k (String) が名前スロットとして存在すれば true。

>>> { a := 1, b := 2 |}.has_name("a")
true

has_position

xs.has_position(i)
has_position: { Int | Bool }

i (Int) が 0 <= i < length なら true。

>>> [10, 20, 30].has_position(1)
true

index_of

xs.index_of(x)
index_of: { a | Option(Int) }

x と == で一致する最初の要素の位置を Some(Int)。無ければ None。

>>> [1, 2, 3].index_of(2)
Some(1)

is_empty

xs.is_empty()
is_empty: { Bool }

length! == 0 と同値。要素が無ければ true。

>>> [].is_empty()
true

join

xs.join(sep)
join: { String | String }

各要素 (すべて String) を sep で連結した String を返す。split の逆操作。

>>> ["a", "b"].join(",")
"a,b"

last

xs.last()
last: { Option(a) }

末尾要素を Some(T) で返す。空なら None。

>>> [1, 2, 3].last()
Some(3)

length

xs.length()
length: { Int }

要素数を返す。

>>> [1, 2, 3, 4].length()
4

map

xs.map(block)
map: { EffectConduit({ a | b }) | List(b) }

各要素を block で変換した新リストを返す。

>>> [1, 2, 3].map {x | x * 2}
[2, 4, 6]

max

xs.max()
max: { Option(a) }

要素 (Comparable) の最大を Some(要素) で返す。空は None。

>>> [1, 2, 3].max()
Some(3)

max_by

xs.max_by { x | key }
max_by: { EffectConduit({ a | b }) | Option(a) }

key (Comparable) が最大の要素を Some で返す。空は None。

>>> ["aa", "b", "ccc"].max_by {x | x.length()}
Some("ccc")

min

xs.min()
min: { Option(a) }

要素 (Comparable) の最小を Some(要素) で返す。空は None。

>>> [3, 1, 2].min()
Some(1)

min_by

xs.min_by { x | key }
min_by: { EffectConduit({ a | b }) | Option(a) }

key (Comparable) が最小の要素を Some で返す。空は None。

>>> ["aa", "b", "ccc"].min_by {x | x.length()}
Some("b")

names

obj.names()
names: { List(String) }

名前スロット名を宣言順で List(String) として返す。

>>> { a := 1, b := 2 |}.names()
["a", "b"]

partition

xs.partition { x | … }
partition: { EffectConduit({ a | Bool }) | (List(a), List(a)) }

述語で要素を 2 分し (満たす, 満たさない) の 2 List をタプルで返す。

>>> [1, 2, 3, 4].partition {x | x % 2 == 0}
([2, 4], [1, 3])

positions

xs.positions()
positions: { List(Int) }

位置インデックス [0, 1, …, length-1] を List(Int) で返す。

>>> [10, 20, 30].positions()
[0, 1, 2]

reverse

xs.reverse()
reverse: { List(a) }

要素を逆順にした新 List を返す (非破壊)。

>>> [1, 2, 3].reverse()
[3, 2, 1]

sort

xs.sort()
sort: { List(a) }

要素を compare で昇順に並べた新 List を返す。要素は Comparable 必須。安定・非破壊。

>>> [3, 1, 2].sort()
[1, 2, 3]

sort_by

xs.sort_by { x | key }
sort_by: { EffectConduit({ a | b }) | List(a) }

key を射影して昇順に並べた新 List を返す。安定・非破壊。

>>> ["ccc", "a", "bb"].sort_by {x | x.length()}
["a", "bb", "ccc"]

sort_with

xs.sort_with { a, b | a.compare(b) }
sort_with: { EffectConduit({ a, a | Ordering }) | List(a) }

block が返す Ordering で並べた新 List を返す。安定・非破壊。

>>> [3, 1, 2].sort_with {a, b | b.compare(a)}
[3, 2, 1]

sum

xs.sum()
sum: { a }

要素 (全 Int または 全 Float) の総和。空は 0。混在・非数値はエラー。

>>> [1, 2, 3].sum()
6

take

xs.take(n)
take: { Int | List(a) }

先頭から最大 n 要素の新 List を返す (非破壊)。n は [0, length] にクランプ。

>>> [1, 2, 3].take(2)
[1, 2]

take_while

xs.take_while { x | … }
take_while: { EffectConduit({ a | Bool }) | List(a) }

先頭から述語が true の間だけ集めた新 List を返す (最初の false で停止)。

>>> [1, 2, 3, 1].take_while {x | x < 3}
[1, 2]

to_list

xs.to_list()
to_list: { List(a) }

要素の新 List (コピー) を返す。

>>> [1, 2, 3].to_list()
[1, 2, 3]

unique

xs.unique()
unique: { List(a) }

== で重複する要素を除いた新 List を返す。出現順を保ち先勝ち (安定)。

>>> [1, 2, 2, 3].unique()
[1, 2, 3]

unlines

xs.unlines()
unlines: { String }

List(String) の各要素に \n を付けて連結する。末尾にも改行が付き、空 List は
空文字列になる。Stringlines の逆変換。

>>> ["a", "b"].unlines()
"a\nb\n"

values

obj.values()
values: { List(Any) }

名前スロットの値を宣言順で List として返す。

>>> { a := 1, b := 2 |}.values()
[1, 2]

zip

xs.zip(other)
zip: { List(b) | List((a, b)) }

other (List) と要素ごとに 2-tuple を組んだ新 List を返す。長さは短い方。

>>> [1, 2].zip([3, 4])
[(1, 3), (2, 4)]

Option メソッド

and_then

o.and_then { x | … }
and_then: { EffectConduit({ a | Option(b) }) | Option(b) }

Some なら内容に block を適用 (block は Option を返す)、None は None (flatMap)。

>>> Some(5).and_then {x | Some(x * 2)}
Some(10)

filter

o.filter { x | … }
filter: { EffectConduit({ a | Bool }) | Option(a) }

Some(v) で述語が true なら Some(v)、false なら None。None はそのまま。

>>> Some(4).filter {x | x > 2}
Some(4)

flatten

o.flatten!

Option(Option(T)) を 1 段ほどく。Some(Some(v))→Some(v)、Some(None)→None、None→None。

>>> Some(Some(7)).flatten!
Some(7)

is_none

o.is_none()
is_none: { Bool }

None なら true。

>>> None.is_none()
true

is_some

o.is_some()
is_some: { Bool }

Some なら true。

>>> Some(1).is_some()
true

map

o.map { x | … }
map: { EffectConduit({ a | b }) | Option(b) }

Some なら内容に block を適用して Some(...)、None は None。

>>> Some(5).map {x | x * 2}
Some(10)

ok_or

o.ok_or(err)
ok_or: { e | Result(a, e) }

Some(v) を Ok(v)、None を Err(err) にして Result へ。

>>> Some(3).ok_or(7)
Ok(3)

or

o.or(default)
or: { a | a }

Some なら内容、None なら default (default は先に評価される)。

>>> None.or(0)
0

or_else

o.or_else { … }
or_else: { EffectConduit({ Option(a) }) | Option(a) }

Some はそのまま、None なら無引数 block を評価する (block は Option を返す)。or の遅延・Option 版。

>>> None.or_else {Some(99)}
Some(99)

unwrap

o.unwrap!
unwrap: { a }

Some なら内容、None なら panic。

>>> Some(5).unwrap!
5

unwrap_or_else

o.unwrap_or_else { … }
unwrap_or_else: { EffectConduit({ a }) | a }

Some なら内容、None なら無引数 block を評価して返す (or の遅延版)。

>>> None.unwrap_or_else { 99 }
99

zip

o.zip(other)
zip: { Option(b) | Option((a, b)) }

Some(a) と Some(b) を Some((a, b)) に組む。どちらかが None なら None。other 非 Option はエラー。

>>> Some(1).zip(Some("a"))
Some((1, "a"))

Ordering メソッド

reverse

o.reverse()
reverse: { Ordering }

Less ⇄ Greater を入れ替え、Equal は不変。降順や向き反転に使う。

>>> Less.reverse()
Greater

then

o.then { block }
then: { EffectConduit({ Ordering }) | Ordering }

o が Equal のときだけ block (無引数、Ordering を返す) を評価して返す。Less / Greater は o をそのまま返す (遅延)。

>>> Equal.then {Less}
Less

Range メソッド

all

r.all { x | … }
all: { EffectConduit({ Int | Bool }) | Bool }

述語が全要素で true なら true。空は true。

>>> range(1, 5).all {x | x > 0}
true

any

r.any { x | … }
any: { EffectConduit({ Int | Bool }) | Bool }

述語が true を返す要素が 1 つでもあれば true。空は false。

>>> range(1, 5).any {x | x > 3}
true

contains

r.contains(x)
contains: { Int | Bool }

x が Int かつ range の要素なら true。実体化せず O(1) 算術判定。

>>> range(1, 5).contains(3)
true

count

r.count { x | … }
count: { EffectConduit({ Int | Bool }) | Int }

述語が true を返す要素の個数を Int で返す。

>>> range(1, 5).count {x | x % 2 == 0}
2

each

r.each(block)

各要素 (Int) に block を適用する。戻り値は ()。

>>> range(1, 4).each {x | println(x)}
1
2
3

filter

r.filter(block)
filter: { EffectConduit({ Int | Bool }) | List(Int) }

block が true を返す要素だけの新 List を返す。

>>> range(1, 5).filter {x | x % 2 == 0}
[2, 4]

find

r.find { x | … }
find: { EffectConduit({ Int | Bool }) | Option(Int) }

述語が最初に true を返す要素 (Int) を Some で返す。無ければ None。

>>> range(1, 5).find {x | x > 2}
Some(3)

first

r.first()
first: { Option(Int) }

先頭要素を Some(Int) で返す。空なら None。

>>> range(1, 5).first()
Some(1)

fold

r.fold(init, block)
fold: { b, EffectConduit({ b, Int | b }) | b }

init から各要素を (acc, 要素) で畳み込んだ単一値を返す。

>>> range(1, 4).fold(0, {acc, x | acc + x})
6

has_position

r.has_position(i)
has_position: { Int | Bool }

i (Int) が 0 <= i < length なら true。

>>> range(1, 5).has_position(1)
true

index_of

r.index_of(x)
index_of: { Int | Option(Int) }

x が range の要素ならその論理位置を Some(Int)。無ければ None。

>>> range(1, 5).index_of(3)
Some(2)

is_empty

r.is_empty()
is_empty: { Bool }

length! == 0 と同値。要素が無ければ true。

>>> range(0, 0).is_empty()
true

last

r.last()
last: { Option(Int) }

末尾要素を Some(Int) で返す。空なら None。

>>> range(1, 5).last()
Some(4)

length

r.length()
length: { Int }

要素数を返す。

>>> range(1, 5).length()
4

map

r.map(block)
map: { EffectConduit({ Int | b }) | List(b) }

各要素を block で変換した新 List を返す。

>>> range(1, 4).map {x | x * 2}
[2, 4, 6]

positions

r.positions()
positions: { List(Int) }

位置インデックス [0, 1, …, length-1] を List(Int) で返す。

>>> range(1, 5).positions()
[0, 1, 2, 3]

reverse

r.reverse()
reverse: { Range }

向きを反転した新 range を返す。

>>> range(1, 4).reverse().to_list()
[3, 2, 1]

step

r.step(n)
step: { Int | Range }

刻み幅を n に設定した新 range を返す。n は正の Int。

>>> range(0, 10).step(2).to_list()
[0, 2, 4, 6, 8]

to_list

r.to_list()
to_list: { List(Int) }

全要素を実体化した List を返す。

>>> range(1, 4).to_list()
[1, 2, 3]

Result メソッド

and_then

r.and_then { v | … }
and_then: { EffectConduit({ a | Result(b, e) }) | Result(b, e) }

Ok なら値に block を適用 (block は Result を返す)、Err は Err (flatMap)。

>>> Ok(5).and_then {v | Ok(v * 2)}
Ok(10)

err

r.err()
err: { Option(e) }

Err(e) を Some(e)、Ok を None にして Option へ (ok の鏡像)。

>>> Err("e").err()
Some("e")

is_err

r.is_err()
is_err: { Bool }

Err なら true。

>>> Err(7).is_err()
true

is_ok

r.is_ok()
is_ok: { Bool }

Ok なら true。

>>> Ok(5).is_ok()
true

map

r.map { v | … }
map: { EffectConduit({ a | b }) | Result(b, e) }

Ok なら値に block を適用して Ok(...)、Err は Err。

>>> Ok(5).map {v | v * 2}
Ok(10)

map_err

r.map_err { e | … }
map_err: { EffectConduit({ e | f }) | Result(a, f) }

Err なら payload に block を適用して Err(...)、Ok はそのまま。Ok 型を変えずエラー型だけ変換する。

>>> Err("boom").map_err {e | "x: " + e}
Err("x: boom")

ok

r.ok()
ok: { Option(a) }

Ok(v) を Some(v)、Err を None にして Option へ (エラーを捨てる)。

>>> Ok(5).ok()
Some(5)

or

r.or(default)
or: { a | a }

Ok なら値、Err なら default (default は先に評価される)。

>>> Err(7).or(0)
0

or_else

r.or_else { e | … }
or_else: { EffectConduit({ e | Result(a, f) }) | Result(a, f) }

Ok はそのまま、Err なら payload e を受ける block を評価 (block は Result を返す)。and_then の Err 側 (flatMap)。

>>> Err("e").or_else {e | Ok(0)}
Ok(0)

unwrap

r.unwrap!
unwrap: { a }

Ok なら値、Err なら panic。

>>> Ok(5).unwrap!
5

unwrap_err

r.unwrap_err!
unwrap_err: { e }

Err なら payload、Ok なら panic。

>>> Err(7).unwrap_err!
7

unwrap_or_else

r.unwrap_or_else { e | … }
unwrap_or_else: { EffectConduit({ e | a }) | a }

Ok なら値、Err なら payload e を受ける block を評価して返す (or の遅延版)。

>>> Err(7).unwrap_or_else {e | e + 1}
8

String メソッド

all

s.all { c | … }
all: { EffectConduit({ String | Bool }) | Bool }

述語が全 rune で true なら true。空は true。

>>> "abc".all {c | c != "z"}
true

any

s.any { c | … }
any: { EffectConduit({ String | Bool }) | Bool }

述語が true を返す rune が 1 つでもあれば true。空は false。

>>> "abc".any {c | c == "b"}
true

code

s.code()
code: { Int }

1 文字 String を Unicode code point (Int) に変換する。長さ ≠ 1 はエラー。

>>> "A".code()
65

contains

s.contains(sub)
contains: { String | Bool }

sub (String) が連続部分列として現れれば true (rune 単位)。

>>> "hello".contains("ell")
true

count

s.count { c | … }
count: { EffectConduit({ String | Bool }) | Int }

述語が true を返す rune の個数を Int で返す。

>>> "abc".count {c | c != "b"}
2

each

s.each(block)

各 rune (長さ 1 String) に block を適用する。戻り値は ()。

>>> "ab".each {c | println(c)}
a
b

ends_with

s.ends_with(suffix)
ends_with: { String | Bool }

末尾が suffix (String) と一致すれば true (rune 単位)。

>>> "hello".ends_with("lo")
true

filter

s.filter(block)
filter: { EffectConduit({ String | Bool }) | List(String) }

block が true を返す rune だけの新 List を返す。

>>> "abc".filter {c | c != "b"}
["a", "c"]

find

s.find { c | … }
find: { EffectConduit({ String | Bool }) | Option(String) }

述語が最初に true を返す rune (長さ 1 String) を Some で返す。無ければ None。

>>> "abc".find {c | c == "b"}
Some("b")

first

s.first()
first: { Option(String) }

先頭の 1 文字 (長さ 1 String) を Some(String) で返す。空なら None。

>>> "hello".first()
Some("h")

fold

s.fold(init, block)
fold: { b, EffectConduit({ b, String | b }) | b }

init から各 rune を (acc, 要素) で畳み込んだ単一値を返す。

>>> "abc".fold("", {acc, c | acc + c})
"abc"

has_position

s.has_position(i)
has_position: { Int | Bool }

i (Int) が 0 <= i < length なら true。

>>> "abc".has_position(1)
true

index_of

s.index_of(sub)
index_of: { String | Option(Int) }

sub が最初に現れる rune 位置を Some(Int)。無ければ None。

>>> "hello".index_of("l")
Some(2)

is_empty

s.is_empty()
is_empty: { Bool }

length! == 0 と同値。rune が無ければ true。

>>> "".is_empty()
true

last

s.last()
last: { Option(String) }

末尾の 1 文字 (長さ 1 String) を Some(String) で返す。空なら None。

>>> "hello".last()
Some("o")

length

s.length()
length: { Int }

rune 数 (Unicode code point 数) を返す。

>>> "hello".length()
5

lines

s.lines()
lines: { List(String) }

s を \n 区切りの行 List に分ける。末尾の改行は空行を生まず、空文字列は
空 List になる。各行から改行文字は取り除かれる。逆変換は Listunlines

>>> "a\nb\n".lines()
["a", "b"]

map

s.map(block)
map: { EffectConduit({ String | b }) | List(b) }

各 rune を block で変換した新 List を返す。

>>> "ab".map {c | c}
["a", "b"]

positions

s.positions()
positions: { List(Int) }

位置インデックス [0, 1, …, length-1] を List(Int) で返す。

>>> "abc".positions()
[0, 1, 2]

repeat

s.repeat(n)
repeat: { Int | String }

s を n 回連結した新 String を返す。n <= 0 は空文字列。

>>> "ab".repeat(3)
"ababab"

replace

s.replace(old, new)
replace: { String, String | String }

old の全出現を new に置換した新 String を返す。

>>> "a-b-c".replace("-", "_")
"a_b_c"

slice

s.slice(start, end)
slice: { Int, Int | String }

半開区間 [start, end) の部分文字列 (rune 単位)。範囲外・逆順はエラー。

>>> "hello".slice(1, 3)
"el"

split

s.split(sep)
split: { String | List(String) }

sep で区切って List(String) を返す。区切りが無ければ単一要素のリスト。

>>> "a,b,c".split(",")
["a", "b", "c"]

splitn

s.splitn(sep, n)
splitn: { String, Int | List(String) }

sep で区切るが最大 n 要素までに制限した List(String) を返す。区切りは高々
n-1 回で、n 要素目に残り全体 (区切り文字を含む) が入る。n が 0 以下なら空リスト。

>>> "a,b,c".splitn(",", 2)
["a", "b,c"]

starts_with

s.starts_with(prefix)
starts_with: { String | Bool }

先頭が prefix (String) と一致すれば true (rune 単位)。

>>> "hello".starts_with("he")
true

to_bytes

s.to_bytes()
to_bytes: { Bytes }

UTF-8 バイト列を Bytes として返す。

>>> "ab".to_bytes()
<2 bytes>

to_float

s.to_float()
to_float: { Option(Float) }

浮動小数化する。成功 Some(Float)、失敗 None。to_int と同じく
前後の空白は受理しない。

>>> "3.5".to_float()
Some(3.5)
>>> " 3.5 ".to_float()
None

to_int

s.to_int()
to_int: { Option(Int) }

整数化する。成功 Some(Int)、失敗 None。桁数の上限はない (任意精度 Int)。
受理するのは 10 進の数の綴りそのものだけで、前後の空白は受理しない
(余白を落とすのは呼び手の責務。s.trim!.to_int! と綴る)。

>>> "42".to_int()
Some(42)
>>> " 42".to_int()
None

to_list

s.to_list()
to_list: { List(String) }

各 rune (長さ 1 String) を要素とする List(String) に展開する。

>>> "ab".to_list()
["a", "b"]

to_lower

s.to_lower()
to_lower: { String }

小文字化した新 String を返す。対応は符号点 1 つが符号点 1 つへ写る単純対応で、文脈や 1 対多の特例は採らない (語末の Σσ になり、İi になる)。

>>> "ABC".to_lower()
"abc"

to_upper

s.to_upper()
to_upper: { String }

大文字化した新 String を返す。対応は符号点 1 つが符号点 1 つへ写る単純対応で、1 対多の特例は採らない (ß は写らずそのまま残る)。

>>> "abc".to_upper()
"ABC"

trim

s.trim()
trim: { String }

先頭・末尾の Unicode 空白を除いた新 String を返す。

>>> "  hi  ".trim()
"hi"

trim_prefix

s.trim_prefix(prefix)
trim_prefix: { String | String }

先頭が prefix と一致すればその分を除いた新 String を、一致しなければ s をそのまま返す。
除去は 1 回だけで、繰り返し剥がさない。

>>> "/usr/bin".trim_prefix("/")
"usr/bin"

trim_suffix

s.trim_suffix(suffix)
trim_suffix: { String | String }

末尾が suffix と一致すればその分を除いた新 String を、一致しなければ s をそのまま返す。
除去は 1 回だけで、繰り返し剥がさない。

>>> "README.md".trim_suffix(".md")
"README"

Tuple メソッド

length

t.length()
length: { Int }

要素数を返す。

>>> (1, 2).length()
2

全型共通 メソッド

apply_when

v.apply_when(cond, f)

cond が真なら f を v に適用した値を、偽なら v をそのまま返す (条件付き適用)。f は v と同じ型を返す関数。偽のとき f は適用されない。f 内の return / break / continue は apply_when を貫いて呼び出し側へ届く。

>>> 5.apply_when(true, { x | x + 1 })
6
>>> 5.apply_when(false, { x | x + 1 })
5

compare

a.compare(b)

全順序で a と b を比較し Ordering (Less / Equal / Greater) を返す。< <= > >= を導出する。

>>> 1.compare(2)
Less

copy

v.copy()

v と同じ現在状態を持つ独立コピーを返す。可変な構造は作り直し、透過的に不変な部分は共有する。overlay オブジェクトを渡すと、その名前スロットの値だけを差し替えた複製を返す (可変性は保持、未知スロットは panic)。

>>> [1, 2].copy()
[1, 2]
>>> { x := 1, y := 2 |}.copy({ y := 20 |}).y
20

inspect

v.inspect()
inspect: { String }

人間可読なデバッグ表現を String で返す (全値で成功)。

>>> [1, 2].inspect()
"[1, 2]"

matches

v.matches(T)

v が型 T に適合すれば true を返す (型テスト)。

>>> 1.matches(Int)
true