Lazy Arrays

From Swym Wiki
Jump to: navigation, search

Arrays in Swym come in two flavours:

  • Eager (normal) arrays - store a sequence of values
  • Procedural arrays - fulfil the same interface as an eager array, except that values are generated on the fly when needed.

(This is 'procedural' in the sense of procedural textures, rather than procedural languages.)

The most commonly-used form of procedural array is the range array:


This does NOT construct a billion-item array, and then extract an item from it! It simply defines a range array with two values: first element = 4, and length = 1000000000. Then, to get the element at index 53, Swym simply computes 53+4.

It's easy enough to define your own procedural arrays, too:

array(length=1000) 'idx'-> { idx*(idx+1) }

Cached arrays lie somewhere between eager and procedural. They will generate values when they need to, but remember them for next time. You can choose a caching strategy - anything from "cache all values" to "cache only the last calculation", based on how the array values are calculated and used.

Personal tools