Files use .rex, the command is rex, and the crate, runtime prefixes, docs, editor files and examples all use the new name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
1.2 KiB
Rexx
69 lines
1.2 KiB
Rexx
// Lists are values: assigning or passing copies them.
|
|
struct Inventory {
|
|
items: []string
|
|
gold: int
|
|
|
|
func add(item: string) {
|
|
items.push(item)
|
|
}
|
|
|
|
func count(): int {
|
|
return items.len()
|
|
}
|
|
}
|
|
|
|
func add_one(xs: []int): []int {
|
|
xs.push(1)
|
|
return xs
|
|
}
|
|
|
|
func first(xs: []int): int {
|
|
return xs[0]
|
|
}
|
|
|
|
func main() {
|
|
var a = [1, 2, 3]
|
|
var b = a
|
|
b[0] = 99
|
|
b.push(4)
|
|
print(a, b)
|
|
|
|
// a function that changes its parameter works on its own copy
|
|
var c = add_one(a)
|
|
print(a, c)
|
|
print(first(a), first(c))
|
|
|
|
// structs holding lists copy deeply
|
|
var inv = Inventory{[], 10}
|
|
inv.add("sword")
|
|
var saved = inv
|
|
inv.add("shield")
|
|
saved.gold = 0
|
|
print(inv)
|
|
print(saved)
|
|
print(inv.count(), saved.count())
|
|
|
|
// nested lists
|
|
var grid = [[1, 2], [3, 4]]
|
|
var snapshot = grid
|
|
grid[0][1] = 20
|
|
grid[1].push(5)
|
|
print(grid)
|
|
print(snapshot)
|
|
|
|
// looping over a list while changing it walks the original
|
|
var xs = [1, 2, 3]
|
|
for x in xs {
|
|
xs.push(x * 10)
|
|
}
|
|
print(xs)
|
|
|
|
// the loop variable is a copy too
|
|
var ys = [1, 2, 3]
|
|
for y in ys {
|
|
y += 100
|
|
print(y)
|
|
}
|
|
print(ys)
|
|
}
|