Iterable - truemedian/Discordia GitHub Wiki

Abstract base class that defines the base methods and/or properties for a general purpose data structure with features that are better suited for an object-oriented environment. Note: All sub-classes must implement their own __init and iter methods. Additionally, more efficient versions of __len, __pairs, and get methods can be redefined in sub-classes.

Methods

get
find
findAll
forEach
random
count
toArray
select

Methods

:get( k )

Returns an individual object by key, where the key should match the result of calling __hash on the contained objects. Operates with up to O(n) complexity.

Name Type
k *

Returns: *

:find( fn )

Returns the first object that satisfies a predicate.

Name Type
fn function

Returns: *

:findAll( fn )

Returns an iterator that returns all objects that satisfy a predicate.

Name Type
fn function

Returns: function

:forEach( fn )

Iterates through all objects and calls a function fn that takes the objects as an argument.

Name Type
fn function

Returns: nil

:random( )

Returns a random object that is contained in the iterable.

Returns: *

:count( fn )

Returns the amount of objects that satisfy a predicate.

Name Type
fn function

Returns: number

:toArray( [ sortBy ], [ fn ] )

Returns a sequentially-indexed table that contains references to all objects. If a sortBy string is provided, then the table is sorted by that particular property. If a predicate is provided, then only objects that satisfy it will be included.

Name Type Optional Optional
sortBy string x
fn function x

Returns: table

:select( ... )

Similarly to an SQL query, this returns a sorted Lua table of rows where each row corresponds to each object in the iterable, and each value in the row is selected from the objects according to the arguments provided.

Name Type
... *

Returns: table

⚠️ **GitHub.com Fallback** ⚠️