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-5.2, LO-5.3, LO-6.1, LO-6.2, LO-6.3, and, revisited, LO-3.1 and LO-4.2
When: after Module 6, as the suggested pace places it
Checking your work: the example tests, your own tests, and the rubric below
Time: about 6 hours, spread over modules 5 and 6

You will write iter, a module of lazy iterators: functions that produce values one at a time, and that combine into pipelines such as “the first five even squares” without building a list in between. You will also write the busted tests that show it works. This is your first Lua project outside a notebook: two files you run with the standalone lua and busted.

What to hand in

Submit these two files through the learning management system:

Getting started

  1. Copy this folder, assignments/pa3/, to a folder of your own. It holds the starter module, iter.lua, whose functions all raise not implemented, and the starter tests, spec/iter_spec.lua, with two examples.

  2. In a terminal, put Lua and busted on your PATH, as setting up describes: source .venv/lua/bin/activate from the repository’s root.

  3. From your copy of the folder, run busted. It finds spec/ and the module on its own. Both example tests fail until you write from_list, count, and take.

Use the notebooks from modules 5 and 6 for trying out ideas, but write the module and the tests in these files: the grader runs them with busted, not in a notebook.

The library

In this assignment, an iterator is a function that returns the next value each time it is called, and nil once it has no more, as the stateful iterators of module 5 do. So each one can go straight into a generic for: for v in iter.from_list({ 1, 2 }) do … end. An iterator never produces nil as a value, but it can produce false.

FunctionReturns
iter.from_list(list)An iterator over the values of the sequence list, in order, false included
iter.count(start, step)An endless iterator over start, start + step, start + 2 * step, …; both default to 1
iter.map(fn, it)An iterator over fn(v) for each value v of it
iter.filter(pred, it)An iterator over the values v of it for which pred(v) is truthy
iter.take(n, it)An iterator over the first n values of it, or all of them if there are fewer
iter.zip(a, b)An iterator whose calls each return two values, the next of a and the next of b; it ends when either ends
iter.split(s, sep)An iterator over the fields of the string s between occurrences of sep, which is plain text, not a pattern
iter.to_list(it)A new sequence of every value of it
iter.memoize(fn)A function that returns fn(x)'s first result, calling fn at most once for each argument x

The details that the tests check:

Errors

Each function checks its arguments when it is called, and raises an error for a wrong one, at level 2, so that the message names the caller’s line. Each message starts with the function’s name:

FunctionRaisesWhen
from_listfrom_list: list must be a tablelist isn’t a table
countcount: start and step must be numberseither one, when given, isn’t a number
countcount: step must not be 0step is 0
mapmap: fn must be a function, map: it must be a functionthe argument named isn’t a function
mapmap: fn returned nilwhile iterating, fn returns nil
filterfilter: pred must be a function, filter: it must be a functionthe argument named isn’t a function
taketake: n must be a non-negative integern isn’t an integer of 0 or more, such as -1, 1.5, or "3"
taketake: it must be a functionit isn’t a function
zipzip: a and b must be functionseither one isn’t a function
splitsplit: s must be a strings isn’t a string
splitsplit: sep must be a non-empty stringsep isn’t a string, or is ""
to_listto_list: it must be a functionit isn’t a function
memoizememoize: fn must be a functionfn isn’t a function
memoizememoize: the argument must not be nil or NaNthe memoized function is called with nil or NaN

Two errors come later, while the result is in use. The function memoize returns checks its argument on each call, and raises its error at level 2 too, naming the line that called it. map: fn returned nil happens inside the iterator, during the loop; the tests check its message but not its position.

Your tests

Write tests for every promise in this handout: each function’s normal results, its edges (an empty list, false values, n = 0, an absent separator), its laziness, and each error and its message. Test laziness with a source that counts its calls, such as:

local calls = 0
local function source()
  calls = calls + 1
  return calls
end

The grader judges your tests the way Lab 6 did: it runs them against a correct iter library, where they must all pass, and against 10 versions of it that each break one promise in this handout, where each version should make at least one of your tests fail. It doesn’t publish the broken versions; work out from the handout what a careless implementation would get wrong.

How it is graded

The course has no grades. This section describes the full check that an instructor or a mentor who reviews your work can run. The hidden tests aren’t published, so when you work alone, test every promise in this handout yourself, and score your work with the rubric.

Hidden tests: 70 points

The hidden tests check the promises above, function by function. Each function’s tests earn its points in proportion to how many of them pass:

FunctionPointsFunctionPoints
from_list5zip8
count5split10
map8to_list4
filter8memoize12
take10Total70

Rubric: 30 points

CriterionFull marksPartialLittle or none
Tests (10)Every test passes on a correct library, and the tests catch at least 9 of the 10 broken versions: 10 points7–8 caught: 7 points; 4–6: 4 points; 1–3: 1 pointNo tests, or none that catch a broken version: 0 points. Each test that fails on a correct library, because it checks something this handout doesn’t promise, costs 2 points
Design (10)Each function does one job. The argument checks share one local helper or one pattern. Every helper is local, the module creates no globals, and each iterator keeps its state in its own closureThe code works, but repeats itself where a helper would do, or puts helpers or state in the module table: 6 pointsGlobals, state shared between iterators, or work done before it is asked for: 3 points or fewer
Style (10)Consistent two-space indentation, descriptive local names, a comment wherever the reason for the code isn’t obvious (such as a placeholder for nil), no dead or commented-out code, and lines of 100 characters or fewerReadable, with a few lapses: 6 pointsHard to follow: 3 points or fewer

Policies

The course’s policies apply. Discuss ideas with anyone, but write your own code and tests, and name everyone you discussed the assignment with on the third line of iter.lua. Generative AI tools may explain concepts and error messages to you, but may not write any part of the module or its tests; say on the second line of iter.lua which tools you used, and how, or write “none”.