User:Pjacobi/Draft/Collections

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The workhorse data structure of script languages is the map, also called "dictionary" (Python), "table" (Lua), "associative array", "hash" or even "object" (ECMAScript), as set of key-value pairs. The underlying implementation most often uses hashing, which has the side effect that iterating through a dictionary follows no order.

The syntax for accessing one item in a map is universially a [x], the languages differ whether accesses using a key not in the map returns some null value or raise an exception.

In most cases maps also implement records and objects, so that a.foo essentially is the same as a ["foo"]. This identity may be exposed as in ECMAScript and Lua or hidden from the surface.

Some languages even implement sequences just as maps which happen to have integer indices, so that the map is the only fundamental data structure in them.


Sequences

[edit | edit source]

Also called "arrays", "vectors" and "tuples".


Lua internally optimize access to map ("table") elements having consecutive integer keys. The only exposed special casing for sequences, which are otherwise normal tables (and so can additionally hold entries with noninteger keys) are a special constructur syntax and the length operator "#".