Class 18 — Wednesday, October 28
A8 first, on the projector. Then week 3’s lists, in Java.
A8 was due last night. A9 goes out today and is due Tuesday, November 3. The reading for Monday is Lists in Java.
Outline
- Your project, from a template — before anything runs
- A list, and the arrow — predict, then run
- Filter and map — pairs, at the keyboard
- Before the break: save to GitHub
- Fold — keyboards down
- Adding up — pairs again
- A list of pictures — keyboards down
- A flag from a list of colors — pairs
- Keep going
- Before you leave: save again
- Before Monday
1. Your project, from a template
This week’s project is a new one, and it is yours, on GitHub.
- Open github.com/cu-cs-labs/cmsc-120-lst and click the green Use this template button → Create a new repository.
- Name it
cmsc-120-lst, the name it has there, and pick Private. Then Create repository. - In IDEA: the menu button (☰, top left) → File → New → Project from Version Control… → paste your new repository’s address, from its page on GitHub → Clone. Log in to GitHub when IDEA asks, and say yes to Load Maven project.
Its src/ has Lst.java, Check.java and Img.java. Do not edit them. Today’s file is a new one beside them, Units.java.
No GitHub account? Clone the template itself the same way, with its address, https://github.com/cu-cs-labs/cmsc-120-lst. You can work in it, but it has nowhere to push: your work stays on this machine.
2. A list, and the arrow
Write your prediction in the Journal before anyone runs anything.
In week 3 you wrote filter(lam(u): u >= 10 end, [list: 4, 10, 20]). Here it is in Java, in a new file Units.java:
void main() {
Lst<Integer> units = Lst.of(4, 10, 20);
IO.println(Lst.filter(u -> u >= 10, units));
IO.println(Lst.map(u -> u * 6, units));
}What do the two lines print?
[list: 10, 20] and [list: 24, 60, 120]. u -> u >= 10 is lam(u): u >= 10 end: a function written in place, its parameter before the arrow and its value after, with no return. Lst.of is [list: ...], and Lst<Integer> says the elements are whole numbers: a Lst is Pyret’s list, empty or a first element linked to the rest. Lists in Java has the rest.
3. Filter and map
- The navigator (not typing) says the examples first. Then they say the steps the function takes.
- The driver types the above, and decides how each step becomes Java.
- If either of you is stuck, drop the roles for a minute and talk it over.
- Swap at every function.
In Units.java. Each function gets a // line above it with a purpose statement and at least two Check.is lines in main, one on the empty list, Lst.of(). Check.is takes two lists: Check.is(outlined(units), Lst.of(10, 20));.
outlined
Lst<Integer> outlined(Lst<Integer> units) — the units a stripe draws as an outline: 10 and up. One line.
Done when checks pass on [4, 10, 20] and on the empty list.
widths
Lst<Integer> widths(Lst<Integer> units) — each stripe’s width, six times its unit. One line.
Done when two checks pass, one on the empty list.
outlineWidths
If you are done, here are two more.
Lst<Integer> outlineWidths(Lst<Integer> units) — the widths of the outlined ones only. outlineWidths(Lst.of(4, 10, 20)) is [list: 60, 120]. Both functions are written already.
Done when checks pass, one on a list where nothing is outlined.
biggerThan
Lst<Integer> biggerThan(Lst<Integer> units, int n) — the units bigger than n. The arrow may use n, a name biggerThan owns: the arrow sits inside biggerThan.
Done when checks at two different n pass.
4. Before the break: save to GitHub
Everyone, before the break.
In IDEA: Git → Commit… (Ctrl+K). Tick your files, write a line saying what you did, then Commit and Push… and Push. Then open your repository on GitHub and check that the commit is there, with your line.
5. Fold
Keyboards stop.
Lst<Integer> units = Lst.of(4, 10, 20);
IO.println(Lst.fold((sum, u) -> sum + u, 0, units));
IO.println(Lst.fold((most, u) -> u > most ? u : most, 0, units));What do the two lines print, and what is the 0 for?
34 and 20. Fold walks the list once, from the front, carrying a value: the 0 is where it starts, then the arrow’s value for 0 and 4, then for that and 10, and so on. Its function takes the value so far first and the element second, as Pyret’s fold did. On the empty list it hands back the 0 itself.
6. Adding up
Pairs, the same way.
total
int total(Lst<Integer> units) — all the units added up. total of the empty list is 0.
Done when two checks pass, one on the empty list.
largest
int largest(Lst<Integer> units) — the largest unit, and 0 for the empty list. A ? : inside the arrow.
Done when checks pass with the largest first, last, and on the empty list.
countOutlined
If you are done, here is one more.
int countOutlined(Lst<Integer> units) — how many units are outlined, counted with a fold and no filter. What does the arrow add for an outlined unit, and what for the rest?
Done when checks pass on [4, 10, 20] and on the empty list.
7. A list of pictures
Keyboards stop. Predict first.
A list can hold any value, pictures too:
Lst<String> colors = Lst.of("red", "green", "blue");
Lst<Img> squares = Lst.map(c -> Img.square(20, "solid", c), colors);
Img row = Lst.fold((soFar, sq) -> Img.beside(soFar, sq),
Img.rectangle(0, 0, "solid", "white"), squares);
IO.println(Lst.length(squares));
IO.println(Img.width(row) + " by " + Img.height(row));
Img.show(row);What do the two lines print, and what shows?
3 and 60 by 20, and three squares in a row: red, green, blue. The map makes a list of three pictures, a Lst<Img>. The fold walks it with a picture as its value so far, starting from a picture of nothing, and puts each square beside what it has: one picture made of many.
Monday’s 67 green squares are this with a list of 67 greens. A function that makes one, copies(67, "green"), is next Monday’s: a list built by hand.
8. A flag from a list of colors
Pairs, the same way, in Units.java.
In week 3 you made a flag from a list of colors: map to turn each color into a stripe, then fold with above to stack them. Now in Java.
stripes
Lst<Img> stripes(Lst<String> colors, int unit) — one stripe per color, each 6 * unit wide and unit high. Lst<Img> is a list of pictures.
Done when Check.is(Lst.length(stripes(Lst.of("red", "white"), 10)), 2); passes, and another at a different length.
flag
Img flag(Lst<String> colors, int unit) — the stripes one above another, the first color on top: a fold with Img.above. Fold needs a picture to start from: Img.rectangle(0, 0, "solid", "white") is a picture of nothing, Pyret’s empty-image.
Done when checks pass on its width and height for four colors, the flag shows, and the empty list gives a height of 0.
9. Keep going
If you are done, here are two more, in Units.java.
columns
Img columns(Lst<String> colors, int unit) — the flag the other way: one stripe per color side by side, each unit wide and 2 * unit high. columns(Lst.of("green", "white", "green"), 20) is Nigeria, 60 by 40. One fold, the stripe made inside its arrow.
Done when checks pass on its width and height for three colors and on the empty list’s width, 0, and Nigeria shows.
target
A ring is a color and a radius. The record goes at the top of the file:
Img target(Lst<Ring> rings) — the rings one on another, the first in the list at the back. Monday’s target is target(Lst.of(new Ring("red", 30), new Ring("white", 20), new Ring("red", 10))). A fold with Img.overlay, which puts its first picture in front of its second. Then swap the two inside the arrow and run it again: what shows, and why?
Done when Monday’s target shows, 60 by 60, and you can say why the swapped one is a single red disc.
10. Before you leave: save again
As before the break, in section 4: commit, push, and check that the commit is on GitHub.
11. Before Monday
A9 is due Tuesday, November 3. The reading, Lists in Java, is due before Monday’s class.