Files
sakurac/tests/programs/text_is_split_lazily_1.sakura
T
2026-12-06 13:52:57 -06:00

28 lines
1.0 KiB
Plaintext

import std::io;
# `split`, `lines`, `words`, and `chars` give their parts one at a time, as views
# of the text, so nothing is made until a caller collects them.
def main(): void {
val csv: strview = "a,bb,,c";
for part in csv.split(',') { print($"[{part}]"); }
print(" ");
val parts := csv.split(',').to_vector();
print($"{parts.length} [{parts[1]}] ");
# A split also walks from its far end.
for part in csv.split(',').rev() { print($"<{part}>"); }
print(" ");
val dash: strview = "-";
print($"{csv.split(',').filter(def(p) => p.length > 0).to_string(dash)} ");
val text: strview = "one two\n three\r\nfour";
print($"{text.lines().count()} {text.words().count()} ");
for line in text.lines() { print($"({line.trim()})"); }
print(" ");
val word: strview = "héllo";
print($"{word.length} {word.chars().count()} ");
var owned := string::new(nothing {});
text.words().filter(def(w) => w.length > 3).into(&mut owned);
print($"{owned} ");
val empty: strview = "";
println($"{empty.split(',').count()} {empty.words().count()}");
}