cs-056 — Pipelines as Functions
| Field | Value |
|---|---|
| id | cs-056 |
| difficulty | Expert |
| topics | functions, composition |
| lecture | lecture-14 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | the full Block I-III toolbox and proof-shaped reasoning |
| objective | make and justify design decisions where the method itself is the challenge |
| solution | held by the instructor; discussed after the attempt |
Problem
Build a text pipeline (clean, tokenise, count, report) where each stage is a function returning the next stage's input. Composition and contracts.
Your deliverable is the function list with contracts, the composed driver, and one input per function that exercises its edge. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- Input: text ' the CAT, the dog! '
- Output: clean -> 'the CAT, the dog!'; tokenise -> ['the','CAT','the','dog']; count -> the:2, dog:1, cat:1
Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.
What to notice
- Each stage returns data for the next; no stage prints. The pipeline is composition: report(count(tokenise(clean(t)))).
- A good function name is a miniature specification.
- Return values compose; printed output does not.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Which cut line would you move, and what argument justifies the boundary?"
Constraints and assumptions
- Difficulty: Expert (independent design decisions and proof-shaped arguments assumed).
- Topics: functions, composition.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for the attack plan; the full argument may exceed the box - outline the proof strategy instead of grinding details.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: Split one function into two and defend the new contract boundary.