Collections and structs
c0de has three ways to group values: the ARRAY is a list, the HASH a dictionary with text keys, the STRUCT a record with named elements. They feel like Python's lists, dicts and dataclasses, with one big difference: they are values. Every assignment, every parameter and every result is a copy, and there are no references, no aliasing and no null.
What you need
A creative world with cheats on (see Tutorials), a trigger block, a wrench, a button, an ARRAY variable item with the subtype INTEGER named numbers, a STRING variable item named text, two STRUCT variable items named a and b and a few uninitialized variable items.
The model
- ARRAY T is a list of values of the type T, numbered from 0. It grows and shrinks by itself; you never declare a size. Arrays of arrays (
ARRAY ARRAY INTEGER) are possible, up to 8 dimensions. - HASH T stores values of the type T under keys, which are always STRINGs. EACH goes through its keys in alphabetical order, not in the order they were added.
- STRUCT declares a type with named elements, in the global part of the program and above everything that uses it. An element can be a struct declared before, which is the only way to nest structs. A struct has no methods.
- Values, not references.
ASSIGN copy original ;copies the whole array, hash or struct. Changingcopynever changesoriginal, and a function never changes the variables of its caller.
| Python | c0de |
|---|---|
a = [] | VARIABLE a ARRAY INTEGER |
a[i] | a[i] |
len(a) | CALL LEN with PARAMETER value a ; |
a.append(x) | ASSIGN a[n] x ; where n is the length of a |
a.insert(i, x) | CALL INSERT |
del a[i], del d[k] | CALL REMOVE with index or key |
a.clear() | CALL CLEAR |
d = {} | VARIABLE d HASH INTEGER |
k in d | CALL HAS_KEY |
for k in sorted(d) | EACH k d |
a, b = b, a | CALL SWAP |
@dataclass class Point | STRUCT point |
p.x | p.x |
Indexes
An index is part of a LABEL: numbers[0], counts[word], stock["gold"], path.from.x. It has up to 8 entries, and each entry is a literal or the name of a variable or constant – never an expression. sorted[next] is fine, sorted[i + 1] is not; compute the position into a variable first.
- Reading a position outside the array raises INDEX_OUT_OF_BOUNDS; there are no negative positions from the end. Reading a key that the hash doesn't have raises UNKNOWN_KEY.
- Assigning to the position right after the last element appends one; a position further out raises INDEX_OUT_OF_BOUNDS. Assigning to a new key adds it to the hash.
- A STRING can be read character by character,
text[i]is a CHAR. It can't be changed through an index, and EACH doesn't go through a STRING.
Arrays, hashes and structs can get start values in a code block after their VARIABLE or CONSTANT line, with one line per entry, like y 64 for the element y of a struct. Only literals are allowed there, no expressions.
The programs
Bubble sort, which returns the sorted array and the number of swaps:
PROGRAM sorting
NOOP
NOOP
FUNCTION bubbleSort
PARAMETER numbers ARRAY INTEGER
RESULT sorted ARRAY INTEGER
RESULT swaps INTEGER
BEGIN
VARIABLE last INTEGER
VARIABLE next INTEGER
VARIABLE swapped BOOLEAN TRUE
ASSIGN sorted numbers ;
CALL LEN
PARAMETER value sorted ;
RESULT result last
WHILE swapped ;
BEGIN
ASSIGN swapped FALSE ;
ASSIGN last last - 1 ;
FOR i INTEGER 0 ; last - 1 ;
BEGIN
ASSIGN next i + 1 ;
IF sorted[i] > sorted[next] ;
BEGIN
CALL SWAP
PARAMETER a sorted[i] ;
PARAMETER b sorted[next] ;
RESULT a sorted[i]
RESULT b sorted[next]
ASSIGN swapped TRUE ;
ASSIGN swaps swaps + 1 ;
END IF
END FOR
END WHILE
END FUNCTIONThe same in Python:
def bubble_sort(numbers: list[int]) -> tuple[list[int], int]:
sorted, swaps = numbers.copy(), 0
last, swapped = len(sorted), True
while swapped:
swapped = False
last -= 1
for i in range(0, last):
if sorted[i] > sorted[i + 1]:
sorted[i], sorted[i + 1] = sorted[i + 1], sorted[i]
swapped = True
swaps += 1
return sorted, swapsCounting words with a HASH:
PROGRAM words
NOOP
INCLUDE c0de/strings
NOOP
FUNCTION countWords
PARAMETER text STRING
RESULT counts HASH INTEGER
RESULT words INTEGER
BEGIN
VARIABLE length INTEGER
VARIABLE word STRING
VARIABLE known BOOLEAN
CALL c0de/strings/LCASE
PARAMETER text text ⊕ " " ;
RESULT result text
CALL LEN
PARAMETER value text ;
RESULT result length
FOR i INTEGER 0 ; length - 1 ;
BEGIN
IF text[i] ≠ ' ' ;
BEGIN
ASSIGN word word ⊕ text[i] ;
END
ELSE
BEGIN
IF word ≠ "" ;
BEGIN
CALL HAS_KEY
PARAMETER hash counts ;
PARAMETER key word ;
RESULT result known
IF NOT known ;
BEGIN
ASSIGN counts[word] 0 ;
END IF
ASSIGN counts[word] counts[word] + 1 ;
ASSIGN words words + 1 ;
ASSIGN word "" ;
END IF
END IF
END FOR
END FUNCTIONTwo structs for points and lines in the world:
PROGRAM points
NOOP
INCLUDE c0de/math
NOOP
STRUCT point
BEGIN
x INTEGER
y INTEGER
z INTEGER
END STRUCT
STRUCT line
BEGIN
from STRUCT point
to STRUCT point
END STRUCT
NOOP
FUNCTION length
PARAMETER path STRUCT line
RESULT result FLOAT
BEGIN
VARIABLE dx INTEGER
VARIABLE dy INTEGER
VARIABLE dz INTEGER
ASSIGN dx path.to.x - path.from.x ;
ASSIGN dy path.to.y - path.from.y ;
ASSIGN dz path.to.z - path.from.z ;
CALL c0de/math/SQR
PARAMETER value dx * dx + dy * dy + dz * dz ;
RESULT result result
END FUNCTION
NOOP
FUNCTION measure
PARAMETER a STRUCT point
PARAMETER b STRUCT point
RESULT path STRUCT line
RESULT distance FLOAT
RESULT above STRUCT point
BEGIN
ASSIGN path.from a ;
ASSIGN path.to b ;
ASSIGN above a ;
ASSIGN above.y above.y + 10 ;
CALL length
PARAMETER path path ;
RESULT result distance
END FUNCTIONRunning them
Bubble sort: set the trigger block to the program sorting and the function bubbleSort. Put the item numbers with the values 5, 3, 8, 1, 9, 2 and one uninitialized item into it. The results are sorted with 1, 2, 3, 5, 8, 9 and swaps 8. An empty array comes back empty, with 0 swaps.
Word count: the function countWords of the program words, with the item text = "The creeper saw the pig and the creeper ran" and one uninitialized item. The result counts is the hash and 1, creeper 2, pig 1, ran 1, saw 1, the 3, and words is 9.
Points: the function measure of the program points. Variable items can hold structs of a program: type STRUCT point into the type field of the item dialog. Put a = (x 0, y 64, z 0), b = (x 3, y 68, z 12) and one uninitialized item into the trigger block. path is the line from a to b, distance is 13.0, and above is (x 0, y 74, z 0).
How it works
- The copy comes free.
ASSIGN sorted numbers ;copies the parameter into the result;numbersis a copy of the caller's array anyway. The sort works onsortedonly. - No expressions in indexes. That's why
bubbleSorthas the variablenext=i + 1and comparessorted[i]withsorted[next]. - Swapping elements. SWAP gets
sorted[i]andsorted[next]as parameters and gives them back swapped into the same two elements. Result targets can have an index too. - Reading characters.
countWordsadds a space at the end, so the last word is counted too, and goes through the text with FOR andtext[i], because EACH only works on arrays and hashes. The checkword ≠ ""skips double spaces. - Keys first, then read. Before
counts[word] + 1reads the key, HAS_KEY checks that it exists, and a new word starts at 0. The key is the value of the variableword. - Nested structs.
linehas two elements of the typepoint, sopointis declared first.path.to.xis an index with two entries: the elementto, then its elementx. - Copies again.
ASSIGN above a ;copiesa, andASSIGN above.y above.y + 10 ;changes only the copy:path.from, also a copy ofa, still has y 64.lengthgets its own copy ofpath.
Differences you'll trip over
- An index is a literal or a variable, never an expression:
list[i + 1]doesn't exist. - Reading a missing key or position is an error, not
Noneorundefined. Use HAS_KEY or LEN first. ASSIGN b a ;copies the whole array, hash or struct. There are no references, also not into a function.=compares two arrays by their content.- LEN, INSERT, REMOVE, CLEAR and SWAP return the changed value as a result. To change a variable, use it as parameter and as result target.
- New elements go only right after the last one. There are no gaps, and position -1 is an error, not the last element.
- Hash keys are always STRINGs, and EACH sorts them alphabetically.
- A STRING is read-only through its index, and EACH doesn't go through it.
- Struct types are declared globally, above their first use. Elements without a start value have the default value of their type.
- Start values of collections are literals only; anything computed needs ASSIGN.
Try it yourself
- Write a function
repeatedthat returns the words of a text that occur more than once, as an ARRAY STRING in alphabetical order. UsecountWords.
Solution
PROGRAM words
NOOP
INCLUDE c0de/strings
NOOP
FUNCTION countWords
PARAMETER text STRING
RESULT counts HASH INTEGER
RESULT words INTEGER
BEGIN
VARIABLE length INTEGER
VARIABLE word STRING
VARIABLE known BOOLEAN
CALL c0de/strings/LCASE
PARAMETER text text ⊕ " " ;
RESULT result text
CALL LEN
PARAMETER value text ;
RESULT result length
FOR i INTEGER 0 ; length - 1 ;
BEGIN
IF text[i] ≠ ' ' ;
BEGIN
ASSIGN word word ⊕ text[i] ;
END
ELSE
BEGIN
IF word ≠ "" ;
BEGIN
CALL HAS_KEY
PARAMETER hash counts ;
PARAMETER key word ;
RESULT result known
IF NOT known ;
BEGIN
ASSIGN counts[word] 0 ;
END IF
ASSIGN counts[word] counts[word] + 1 ;
ASSIGN words words + 1 ;
ASSIGN word "" ;
END IF
END IF
END FOR
END FUNCTION
NOOP
FUNCTION repeated
PARAMETER text STRING
RESULT words ARRAY STRING
BEGIN
VARIABLE counts HASH INTEGER
VARIABLE next INTEGER
CALL countWords
PARAMETER text text ;
RESULT counts counts
EACH word counts
BEGIN
IF counts[word] > 1 ;
BEGIN
ASSIGN words[next] word ;
ASSIGN next next + 1 ;
END IF
END EACH
END FUNCTIONFor "The creeper saw the pig and the creeper ran", words is creeper, the. EACH delivers the keys in alphabetical order, and next is always the length of words, so words[next] appends.
- Remove the HAS_KEY call and the IF after it from
countWords. What happens?
Solution
The process crashes with UNKNOWN_KEY at the first word: counts[word] + 1 reads a key that doesn't exist yet. A hash has no default value for missing keys.
Read more
- The language: Types, Variables and constants, Structs, ASSIGN and EACH loops
- Libraries: Changing values
- Handbook Functions: LEN, HAS_KEY, REMOVE, INSERT, CLEAR and SWAP
- Handbook Data: TYPE_ARRAY, TYPE_HASH and TYPE_STRUCT