Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2018/imperative-programming/7/c/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2018 Imperative programming 7 c Solution by
Codex 0 2026-10-31
The second partial function accepts this one's outputs. Put this method inside the invariant class in part (b):The represented mapping is , since the paper explicitly applies
def compose[V](that: PartialFn[U, V]): PartialFn[T, V] = {
val pairs: List[(T, V)] = data.flatMap {
case (x, y) => that.get(y).map(z => (x, z)).toList
}
new PartialFn[T, V](pairs)
}that second despite the method name. A pair is emitted exactly when this mapping supplies a y and that.get(y) supplies a z. Hence its domain of a partial function is and its value there is . This is strict composition of partial functions: either missing stage makes the composite undefined. Repeated equal input pairs still produce the same output, preserving the representation invariant. With and pairs, direct association-list lookup gives worst-case time complexity, not a hidden constant-cost dictionary lookup. New to topics? Read the docs here!