@eryx/iter Module

Utilities for working with sequence-like data through a small iterator API.

Functions in this module accept three kinds of iterable input:

  1. Dense array-like tables such as { "a", "b", "c" }
  2. Simple iterator functions of the form () -> (index?, value)
  3. Tables or userdata with an __iter metamethod

Most transforming helpers return a SimpleIterator, which is a stateful iterator function yielding (index, value) pairs until it returns nil.

Use this module when you want to write code against "something iterable" without caring whether the source is an array, an iterator callback, or a custom object with __iter.

Sequence-style transforms such as filter, unique, and reverse produce dense array-like results or dense iterator indices. flatten, by contrast, preserves the yielded numeric keys from the underlying iterable.

The iter.Iterable<T, any> type can be used to describe any iterable sequence that results in items of type T. iter.SimpleIterator<T> on the other hand describes the iterator types returned by these functions.

local iter = require("@eryx/iter")

local evens = iter.filter({ 1, 2, 3, 4, 5, 6 }, function(value)
    return value % 2 == 0
end)

for index, value in evens do
    print(index, value) -- 1 2, then 2 4, then 3 6
end
local iter = require("@eryx/iter")

local values = iter.reverse(
    iter.unique(
        iter.map({ "pear", "apple", "pear", "plum" }, string.upper)
    )
)

print(values) -- { "PLUM", "APPLE", "PEAR" }

Summary

Classes

Functions

iter.map<T, U, I>(iterable: Iterable<T, I>, mapper: ((T) → U))SimpleIterator<U>
iter.filter<T, I>(iterable: Iterable<T, I>, filter: ((T) → boolean))SimpleIterator<T>
iter.forEach<T, I>(iterable: Iterable<T, I>, callback: ((T) → ()) | ((T, number) → ()))()
iter.flatten<T, I>(iterable: Iterable<T, I>){ T }
iter.tableIterator<T>(array: { T })SimpleIterator<T>
iter.reverse<T, I>(iterable: SimpleIterator<T> | MetaIterator<T, I>, inPlace: false?){ T }
iter.unique<T, I>(iterable: Iterable<T, I>)SimpleIterator<T>
iter.find<T>(iterable: { T }, match: ((T) → boolean), start: number?)(number?, T)
iter.reduce<T, U, I>(iterable: Iterable<T, I>, reducer: ((U, T) → U), initial: U)U
iter.all<T, I>(iterable: Iterable<T, I>, match: ((T) → boolean))boolean
iter.any<T, I>(iterable: Iterable<T, I>, match: ((T) → boolean))boolean

API Reference

Classes

MetaIterator<T, Invariant>

Any table that has an __iter metamethod

Properties

Functions

iter.map

Lazily maps each item in an iterable.

iter.map<T, U, I>(iterable: Iterable<T, I>, mapper: ((T) → U))SimpleIterator<U>

Parameters

iterable: Iterable<T, I>

Iterable source to read from.

mapper: ((T) → U)

Transform applied to each yielded value.

Returns

Iterator that yields the original index with the mapped value.

iter.filter

Lazily filters an iterable by predicate. Matching items are reindexed densely from 1.

iter.filter<T, I>(iterable: Iterable<T, I>, filter: ((T) → boolean))SimpleIterator<T>

Parameters

iterable: Iterable<T, I>

Iterable source to read from.

filter: ((T) → boolean)

Predicate used to decide whether an item should be yielded.

Returns

Iterator that yields only items matching filter.

iter.forEach

Calls a function on every item in an iterable

iter.forEach<T, I>(iterable: Iterable<T, I>, callback: ((T) → ()) | ((T, number) → ()))()

Parameters

iterable: Iterable<T, I>

Iterable source to read from.

callback: ((T) → ()) | ((T, number) → ())

Callback called with each yielded value and its index.

iter.flatten

Materializes an iterable into an array. Table inputs are returned directly without cloning. Iterator inputs preserve their yielded numeric keys, which may produce a sparse table.

iter.flatten<T, I>(iterable: Iterable<T, I>){ T }

Parameters

iterable: Iterable<T, I>

Iterable source to flatten.

Returns

{ T }

Array containing all yielded values, keyed by their yielded indices.

iter.tableIterator

Converts an array into a stateful iterator function.

iter.tableIterator<T>(array: { T })SimpleIterator<T>

Parameters

array: { T }

Array to iterate over.

Returns

Iterator that yields sequential array indices and values.

iter.reverse

Returns a reversed array. Table inputs may be reversed in place; iterator inputs are collected densely first.

iter.reverse<T, I>(iterable: SimpleIterator<T> | MetaIterator<T, I>, inPlace: false?){ T }

Parameters

iterable: SimpleIterator<T> | MetaIterator<T, I>

Iterable source to reverse.

Returns

{ T }

Reversed array.

iter.reverse<T>(iterable: { T }, inPlace: boolean?){ T }

Parameters

iterable: { T }

Iterable source to reverse.

inPlace: boolean?

When true reverse the table in place.

Returns

{ T }

Reversed array.

iter.unique

Lazily yields only the first occurrence of each distinct value. Yielded items are reindexed densely from 1. Duplicate detection is based on direct Lua equality and table-key semantics.

iter.unique<T, I>(iterable: Iterable<T, I>)SimpleIterator<T>

Parameters

iterable: Iterable<T, I>

Iterable source to deduplicate.

Returns

Iterator that skips values that have already been seen.

iter.find

Finds the first item matching a predicate. For table inputs, start can be used to resume searching from a later index. For iterator inputs, calling this consumes items until a match is found or the iterator ends.

iter.find<T>(iterable: { T }, match: ((T) → boolean), start: number?)(number?, T)

Parameters

iterable: { T }

Iterable source to search.

match: ((T) → boolean)

Predicate used to test each item.

start: number?

Starting index.

Returns

Matching index and value, or nil if no match is found.

iter.find<T, I>(iterable: SimpleIterator<T> | MetaIterator<T, I>, match: ((T) → boolean))(number?, T)

Parameters

iterable: SimpleIterator<T> | MetaIterator<T, I>

Iterable source to search.

match: ((T) → boolean)

Predicate used to test each item.

Returns

Matching index and value, or nil if no match is found.

iter.reduce

Reduces an iterable into a single value.

iter.reduce<T, U, I>(iterable: Iterable<T, I>, reducer: ((U, T) → U), initial: U)U

Parameters

iterable: Iterable<T, I>

Iterable source to reduce.

reducer: ((U, T) → U)

Accumulator function called with the current reduced value and next item.

initial: U

Initial accumulator value.

Returns

U

Final reduced value.

iter.all

Returns whether every item in the iterable matches a predicate.

iter.all<T, I>(iterable: Iterable<T, I>, match: ((T) → boolean))boolean

Parameters

iterable: Iterable<T, I>

Iterable source to test.

match: ((T) → boolean)

Predicate used to test each item.

Returns

true when all yielded items match, otherwise false.

iter.any

Returns whether any item in the iterable matches a predicate.

iter.any<T, I>(iterable: Iterable<T, I>, match: ((T) → boolean))boolean

Parameters

iterable: Iterable<T, I>

Iterable source to test.

match: ((T) → boolean)

Predicate used to test each item.

Returns

true when any yielded item matches, otherwise false.

Types

SimpleIterator<T>

A stateful iterator function yielding (index, value) pairs until it returns nil.

type SimpleIterator<T> = (() → (number?, T))

Iterable<T, Invariant>

An array, a SimpleIterator, or any value with an __iter metamethod.

type Iterable<T, Invariant> = { T } | SimpleIterator<T> | MetaIterator<T, Invariant>