Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Objectives: LO-9.1, LO-9.2, LO-9.3
Prerequisites: the Module 9 reading and Lab 8
Time: about 2 hours

You will trace values through resume and yield, write a recursive generator of permutations, add two stages to a pipeline, extend the reading’s scheduler with start delays and a time limit, and watch one task that never yields hold up all the others. As in the earlier labs, each check cell prints passed or fails with a message, and the checks fail until you finish their exercise.

Setup

Section 5 measures real time, so its numbers depend on your machine; everything else is the same on every run. The second cell prints the Lua version, which should be Lua 5.4.

1. Values in and out

make_echo makes a coroutine that multiplies, decorates, and adds what it is given. Read it, and don’t run anything yet.

Exercise L9.1 (LO-9.1, ★★, about 15 minutes)

The check makes two echo coroutines, co and co2, and evaluates the expressions below in order. Predict what each one gives, as all its values in order, separated by a comma and a space: for example "true, 40" or "suspended". Make all eleven predictions before you run the check.

2. A recursive generator

Exercise L9.2 (LO-9.2, ★★★, about 25 minutes)

Write permutations(list), which returns a generator, for the generic for, of every ordering of the items of list. Each ordering it gives is a new table, so the loop may keep it, and list itself is unchanged when the loop ends. The orderings may come in any order.

Hint 1

Write a recursive helper place(k) that fills position k with each item not yet placed in turn, and calls place(k + 1); at k = #list + 1 an ordering is complete, so yield a copy of it.

Hint 2

A neat way to try each item at position k is to swap it into place, recurse, and swap it back: work on a copy of list, and swap copy[k] with copy[i] for each i from k to the end.

3. A pipeline

The next cell defines the reading’s producer, lines_of, and a source that counts how many times it has been asked for a value.

Exercise L9.3 (LO-9.2, ★★, about 20 minutes)

Write two pipeline stages with coroutine.wrap, each taking a source, a function that returns the next value or nil:

  • numbered(source) gives each value as the string "n: value", counting from 1.

  • chunked(size, source) gives the values in tables of size consecutive values; the last table may be shorter, and an empty source gives nothing. It must ask its source for no more values than the chunk it is building needs.

4. A scheduler with start delays and a time limit

The next cell holds the reading’s scheduler state and sleep, and a log that tasks write to.

Exercise L9.4 (LO-9.3, ★★★, about 30 minutes)

Write two global functions:

  • spawn(fn, delay) schedules a new task, a coroutine running fn, to start at now + delay; delay defaults to 0.

  • run(until_time) repeatedly takes the waiting task with the earliest wake time, sets now to that time, and resumes it. A task that yields is put back with the wake time now plus the delay it yielded; a task that finishes is dropped; a task that raises an error stops run with that error. Among tasks with the same wake time, the one that was put in waiting first runs first. run stops when no task is waiting, or when the earliest wake time is later than until_time; with no until_time, it runs until no task is waiting. It returns the number of resumes it made.

Hint

Start from the reading’s run. Before removing the earliest entry, compare its wake time with until_time, and stop without removing it if it is later.

5. A task that never yields

The next cell runs three tasks on your scheduler, and records the real time, from os.clock, at which each line is logged. The task busy computes for about half a second without calling sleep. Before you run it, predict: when does quick, which is due at simulated time 1, write its line, in real time?

Your answer here: quick was due at simulated time 1. Why did it run only after busy finished, and what change to busy would let quick run on time?

Summary

  • resume’s arguments arrive as the function’s parameters, then as yield’s results; yield’s arguments and the function’s returns come back from resume, after true, or after false and the message when the coroutine fails.

  • A recursive generator keeps its paused calls on the coroutine’s stack, so it needs no explicit stack of its own.

  • Pipeline stages built with wrap take a source and are one; the consumer pulls each value through, and no stage reads ahead of what it needs.

  • A scheduler resumes the earliest task, moves its clock, and reschedules what yields; it works only as long as every task yields often.

Next, do the Module 9 practice, then start PA4.