Pattern aliases
The as
operator can be used to assign sub patterns to variables.
as
演算子を使うと、サブパターンを変数に代入することができます。
The pattern [_, ..] as it
will match any non-empty list and
assign that list to the variable it
.
パターン [_, ..] as it
は、空でないリストにマッチし、そのリストを変数 it
に代入します。
パターン [[_, ..] as first, ..]
は、lists
の最初の要素が [_, ..] as first
にマッチするか、つまり最初の要素が空でないリストかどうか、をチェックしています。
import gleam/io
pub fn main() {
io.debug(get_first_non_empty([[], [1, 2, 3], [4, 5]]))
io.debug(get_first_non_empty([[1, 2], [3, 4, 5], []]))
io.debug(get_first_non_empty([[], [], []]))
}
fn get_first_non_empty(lists: List(List(t))) -> List(t) {
case lists {
[[_, ..] as first, ..] -> first
[_, ..rest] -> get_first_non_empty(rest)
[] -> []
}
}