Lua Academy · Lesson

Stateless Iterators

Write stateless iterators like ipairs and implement custom variants.

Lesson 2 of 413 steps

Stateless Iterators is a free Lua Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Stateless Iterators” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Stateless Iterators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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