Documentation
Bop language guide

Arrays

Arrays๐Ÿ”—

Arrays are ordered, mutable collections that can hold any mix of types.

Creating arrays๐Ÿ”—

let items = [1, 2, 3]
let empty = []
let mixed = [1, "two", true, none]

Accessing elements๐Ÿ”—

Arrays are 0-indexed. Negative indices count from the end:

let items = [10, 20, 30]
print(items[0])     // 10
print(items[2])     // 30
print(items[-1])    // 30 (last element)
print(items[-2])    // 20

Out-of-bounds access produces an error.

Modifying elements๐Ÿ”—

let items = [10, 20, 30]
items[0] = 99
print(items)    // [99, 20, 30]

Methods๐Ÿ”—

Mutating methods such as push, pop, insert, remove, reverse, and sort write their updated array back to a variable receiver. Nested index and field receivers are not write-back places yet: dict["items"].push(value) and holder.items.sort() raise a runtime error with the workaround rather than silently doing nothing. Use an explicit variable and assignment:

let items = dict["items"]
items.push(value)
dict["items"] = items

True temporary receivers remain valid. [1, 2].push(3) mutates the temporary, discards it, and returns none.

MethodReturnsDescription
arr.len()intNumber of elements
arr.push(val)noneAppend to end
arr.pop()valueRemove and return last element
arr.has(val)boolWhether the array contains the value
arr.index_of(val)intIndex of first occurrence, or -1
arr.insert(i, val)noneInsert at a signed index, shifting right. Negative indices count from the end; len appends
arr.remove(i)valueRemove at a signed index. Negative indices count from the end
arr.slice(start, end)arrayHalf-open sub-array. Negative bounds count from the end; out-of-range bounds clamp
arr.reverse()noneReverse in place
arr.sort()noneSort in place
arr.join(sep)stringJoin elements into a string

Plus the universal arr.type(), arr.to_str(), arr.inspect().

Practical examples๐Ÿ”—

Building a list๐Ÿ”—

let squares = []
for i in range(1, 6) {
  squares.push(i * i)
}
print(squares)    // [1, 4, 9, 16, 25]

Filtering values๐Ÿ”—

let numbers = [3, 7, 1, 9, 4, 6, 2, 8]
let big = []
for n in numbers {
  if n > 5 {
    big.push(n)
  }
}
print(big)    // [7, 9, 6, 8]

Checking membership๐Ÿ”—

let allowed = ["admin", "editor", "viewer"]
let role = "editor"
if allowed.has(role) {
  print("Access granted")
} else {
  print("Access denied")
}

Sorting and joining๐Ÿ”—

let scores = [42, 17, 85, 3]
scores.sort()
print(scores)    // [3, 17, 42, 85]

let names = ["Charlie", "Alice", "Bob"]
names.sort()
print(names.join(", "))    // "Alice, Bob, Charlie"
Found something unclear?Edit this page on GitHub
Documentation

Search Bop

Start typing to search the guide and reference.