0Pricing
Lua Academy · Lesson

Stateless Iterators

Write stateless iterators like ipairs and implement custom variants.

Stateless Iterators is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Stateless Iterator?

A stateless iterator carries no internal state. All information is passed as arguments on each call: the invariant state and the current control variable.

ipairs: The Canonical Example

ipairs is stateless — it uses the table as state and the current index as the control variable.

local function myIpairs(t, i)
  i = i + 1
  local v = t[i]
  if v ~= nil then return i, v end
end
-- Use: for i,v in myIpairs, t, 0 do

squares Iterator

A stateless iterator that returns successive squares. The state holds the limit, the control variable is the current number.

local function squaresIter(max, n)
  n = n + 1
  if n <= max then return n, n*n end
end
for n, sq in squaresIter, 5, 0 do
  print(n, sq)
end

No Closure Needed

Stateless iterators avoid closure allocation. Since all state is passed as arguments, there is zero heap allocation per iteration.

Implementing pairs-style Iterator

A stateless key-value iterator using next as the iterator function — exactly how pairs works.

for k, v in next, {a=1, b=2} do
  print(k, v)
end

next Function

next(t, k) returns the next key-value pair after key k. Pass nil (or omit) to start from the beginning.

local t = {x=10, y=20}
local k, v = next(t, nil)
print(k, v)
k, v = next(t, k)
print(k, v)

Custom Range Iterator

A stateless range iterator with step. State holds a config table; control is current value.

local function rangeIter(state, n)
  n = n + state.step
  if n <= state.limit then return n end
end
for v in rangeIter, {limit=10, step=2}, 0 do
  print(v)
end

Performance Advantage

Stateless iterators are ideal for tight loops. No closure is created per call, making them faster than stateful closure iterators for simple traversals.

Limitations

When iteration requires complex state (multiple counters, queues, or dynamic data), stateless iterators become awkward. Use stateful closure iterators or coroutines instead.

Combining with table.move

A stateless slice iterator returns elements from a table slice. The invariant state holds the array and end index; control is the current index.

Standard Library Stateless Iterators

ipairs, pairs (via next), and string.gmatch internally use stateless or nearly-stateless designs.

Stateless Iterator Question

What makes an iterator stateless?

Recap: Stateless Iterators

Stateless iterators receive all context as arguments, require no closure allocation, and are highly performant. They are ideal for simple sequential traversals like array indexing.

Frequently asked questions

Is the “Stateless Iterators” lesson free?

Yes — the full text of “Stateless Iterators” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Stateless Iterators”?

Write stateless iterators like ipairs and implement custom variants. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Stateless Iterators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Generic for Protocol
  2. Stateless Iterators
  3. Stateful Iterators with Closures
  4. Coroutine-Based Generators
← Back to Lua Academy