Like many programming languages, Experimentor supports the idea of a collection of items. Collections are very powerful and allow you to do all sorts of wonderful things.
Unlike many other programming languages, Experimentor collections are very easy to use while being simultaneously very powerful.
Defining a collection
Defining a collection is done in almost the same way as defining anything else in Experimentor:
1 2 3 |
define collection ColourCollectionType // Items in the collection go here, one per line end collection |
There are however a few important difference when defining a collection of things and defining other types:
- There is no “without persistence” on a collection. Collections do not (currently) support any type of persistence, so indicating that you don’t want persistence on a collection is rather redundant.
- When defining the list of things inside a collection there are no define statements or assignment statements, just a list of values.
For example, let us define a collection of different colours:
1 2 3 4 5 6 7 8 |
define collection ColourCollectionType #FF0000 // Red #FF0000 // Red occurs twice, because I like red #00FF00 // Green #0000FF // Blue #000000 // Black #FFFFFF // White end collection |
Or perhaps define a collection of strings:
1 2 3 4 5 6 7 |
define collection MessagesCollectionType "Hello mom!" "Hello dad!" "Hello big wide world" "Goodbye everybody" "So long, and thanks for all the fish" end collection |
Notice that in all cases, we are not using the “define” keyword inside the collection – we are just providing a collection of data values.
Variables inside the collection definition
Of course, we can use variables inside our collection definition like so:
1 2 3 4 5 6 7 8 9 |
define red = #FF0000 define greed = #00FF00 define collection ColourCollectionType red green #0000FF // Blue #000000 // Black #FFFFFF // White end collection |
However – and this is a little tricky – these variables will not be “resolved” until the collection is actually created. That is, the data value inside the variable is not inserted into the list until you actually create a copy of the collection.
Creating shapes inside your collection
It’s also possible to create collections of objects (such as shapes, text or images). For example, the following code creates a collection of sound objects:
1 2 3 4 5 6 7 8 9 |
define collection ShapesCollectionType create circle @ 10%,20% with radius=10%, isVisible=true, colour=#FF0000 create rectangle @ 30%,20% with width=100, height=100, isVisible=true, colour=#00FF00 create text @ 10%,40% with value = "Hello world", isVisible=true, colour=#0000FF create image @ 50%,40% with source = "d:\tmp\TestImage.bmp", isVisible=true end collection define shapes = create ShapesCollectionType wait for 5 sec |
The above program creates a collection of different shapes, and because they all had isVisible=true the wait command on the last line will display all these shapes for 5 seconds. (Note that without creating the collection using define shapes = create ShapesCollectionType there wouldn’t be anything visible, because we would only have defined a type/template for a collection, but never have actually created such a collection).
Creating sounds inside your collection
It’s also possible to create collections of sounds; for example, the following code creates a collection of sound objects:
1 2 3 4 5 6 7 8 |
define collection SoundsCollectionType create sound "d:\sounds\Test1.wav" create sound "d:\sounds\Test2.wav" create sound "d:\sounds\Test3.wav" create sound "d:\sounds\Test4.wav" create sound "d:\sounds\Test5.wav" end collection define sounds = create SoundsCollectionType |
Creating your collection
Just like rectangles, circles, and devices, defining a collection does not actually create any copies of that collection – it just defines the collection “type”.
To actually create a collection instance that we can start doing things with we need to use the “create” keyword:
1 2 |
// Create our collection define colours = create ColourCollectionType |
This gives us an actually instance of our “ColourCollectionType” collection definition inside the “colours” variable (“data bucket”). Now we can actually use our list to do interesting things.
Quickly creating an empty collection
It’s possible to use a short-cut to create a new empty collection, without first defining a collection type:
1 |
define myEmptyCollection = create empty collection |
This allows you to then add items to the collection using the collection operators.