Building text
Building text
Text is bytes, and bytes cannot be changed, so text is built by joining pieces into new bytes.
fn main() -> int {
let names = bytes_cons(head: "mira", tail: bytes_cons(head: "tomas", tail: one_bytes("ines")));
print(concat3("players: ", joined(sorted(names), ", "), "\n"));
print(concat5("rounds: ", int_to_text(4), ", best: ", int_to_text(24), "\n"))
}bootstrap/qdx run std/*.qdx text.qdxplayers: ines, mira, tomas
rounds: 4, best: 24The pieces
concatjoins two pieces, andconcat3,concat4andconcat5join more in one call.int_to_textturns a number into its digits. Nothing converts a number to text automatically.joinedputs a separator between the items of abytes_list, andsortedorders them first.byte(10)makes one byte from its number, for the rare byte that has no escape.
Cost
Every concat copies both sides into new bytes. Adding one small piece at a time to a long text copies the long part again on each step, so the work grows with the square of the length. For a few lines of output that does not matter.
For long output, do not build it at all. Print each piece as you reach it, as the score program does with one print per player.
Line ends
print adds nothing. A line end is the \n you write.