15. Programs that print
dbg is for you while you write code: it shows the code, its line and its
value. A program’s output for the people who use it is printed with
io.print, which shows only the value:
import std::io
fn main()
io.print("Shopping list")
["eggs", "milk", "bread"] |> Iter.each(fn item -> io.print("- ${item}"))
endShopping list
- eggs
- milk
- bread
Two things are new here:
import std::ioat the top lets the file useio, Nomi’s tools for input and output, andio.printprints one line.Iter,StringandMapare in every file, but most code doesn’t print, so a file that does asks forio.Iter.eachcalls its function once for each value, for what the function does rather than a value it gives back.
1..=5 is a range, the numbers from 1 to 5 with both ends included. It
works with Iter functions like a list does:
fn main()
dbg 1..=5 |> Iter.map(fn n -> n * n) |> Iter.to_list()
enddbg line 2: 1..=5 |> Iter.map(fn n -> n * n) |> Iter.to_list() = [1, 4, 9, 16, 25]
Your turn
Section titled “Your turn”times_table should give back the lines of a times table, from 1 up to 5:
for 3 that is "3 x 1 = 3", "3 x 2 = 6" and so on, up to
"3 x 5 = 15". Right now it gives back only the first line.
A test can’t see what a program prints, so the task builds the lines, and
a program prints them with times_table(3) |> Iter.each(io.print).
io.print is passed by its name, as String.uppercase was in the lesson
on pipes.
fn times_table(by: Int) -> List<String>
["${by} x 1 = ${by}"]
end
// The checks below run when you press Check. You don't need to change them.
test "the table of 3"
assert times_table(3) == ["3 x 1 = 3", "3 x 2 = 6", "3 x 3 = 9", "3 x 4 = 12", "3 x 5 = 15"]
end
test "the table of 10"
assert times_table(10) == [
"10 x 1 = 10",
"10 x 2 = 20",
"10 x 3 = 30",
"10 x 4 = 40",
"10 x 5 = 50",
]
end
Map the range 1..=5 to a line for each n: "${by} x ${n} = ${by * n}". Then collect the lines with Iter.to_list().
fn times_table(by: Int) -> List<String> 1..=5 |> Iter.map(fn n -> "${by} x ${n} = ${by * n}") |> Iter.to_list() end // The checks below run when you press Check. You don't need to change them. test "the table of 3" assert times_table(3) == ["3 x 1 = 3", "3 x 2 = 6", "3 x 3 = 9", "3 x 4 = 12", "3 x 5 = 15"] end test "the table of 10" assert times_table(10) == [ "10 x 1 = 10", "10 x 2 = 20", "10 x 3 = 30", "10 x 4 = 40", "10 x 5 = 50", ] end