Reference

Arrays

Every built-in property and method of arrays, grouped by what it does, with which ones change the array in place.

Ordered lists, typed int[], string[] or array. Positions count from 1. Lesson: Arrays.

main.osl
int[] a = [5, 3, 8, 1]
a.append(9)
log a.len a[1] a[-1]
log a.sum() a.max()
log a.map(n -> n * 2).filter(n -> n > 8)
Click the code to edit it

Properties, indexing and operators#

FormGives
a.lennumber of items (a property, no brackets)
a[n]the item at position n (1 is the first, -1 the last)
a[n] = xreplaces the item at position n
a[from:to]the items from from to to, both included; leave either out for the start or end
a ++ ba new array of the items of a then b
[...a, x]a new array with the items of a spread in
x in atrue if x is an item
a == btrue if both hold equal items in the same order
1 to 5the array [1, 2, 3, 4, 5]

Position 0 is an error. Reading past the end gives null (or the zero value of the item type, such as 0, in a typed array).

main.osl
int[] a = [10, 20, 30, 40]
log a.len a[1] a[-1]
log a[2:3] a[:2] a[3:]
a[1] = 11
log a ++ [50]
log [...a, 0]
log 20 in a
log 1 to 4
Click the code to edit it
main.osl
int[] a = [10, 20]
log a[0]
Click the code to edit it

Adding and removing#

MethodDoes
append(x, ...)adds each x to the endin place
prepend(x, ...)adds each x to the startin place
insert(n, x, ...)puts the values in before position nin place
delete(n)removes the item at position n; out of range does nothingin place
pop()removes the last item and returns itin place
shift()removes the first item and returns itin place
fill(x)sets every item to xin place
resize(n)cuts to n items, or adds empty items up to nin place
swap(a, b)exchanges the items at positions a and bin place
concat(other)returns a new array of bothcopy
main.osl
int[] a = [1, 2, 3]
a.append(4, 5)
a.prepend(0)
a.insert(3, 99)
log a
a.delete(3)
log a.pop() a.shift()
log a
a.swap(1, 2)
log a
a.resize(5)
log a
a.fill(7)
log a
log [1, 2].concat([3])
Click the code to edit it

Looking inside#

MethodGives
first(), last()the first or last item
at(n)the item at position n (negative counts from the end), or null
isEmpty()true if there are no items
contains(x)true if x is an item
containsAny(x, y, ...)true if any of the values is an item
index(x)position of the first x, or 0
lastIndex(x)position of the last x, or 0
find(fn)the first item for which fn is true
findIndex(fn)the position of that item
count(fn)how many items fn is true for
some(fn)true if fn is true for at least one item
every(fn)true if fn is true for every item
main.osl
int[] a = [4, 8, 15, 16, 23, 42]
log a.first() a.last() a.at(2) a.at(-2) a.at(99)
log a.contains(15) a.index(15) a.index(99) a.containsAny(1, 42)
log a.find(n -> n > 10) a.findIndex(n -> n > 10)
log a.count(n -> n % 2 == 0)
log a.some(n -> n > 40) a.every(n -> n > 5)
Click the code to edit it

count takes a function. For the number of items use .len.

Transforming#

None of these change the array.

MethodGives
map(fn)a new array of fn(item) for each item
filter(fn)a new array of the items for which fn is true
flatten()the nested arrays opened one level
chunk(n)an array of arrays of n items each
zip(other)an array of [item, otherItem] pairs
groupBy(fn)an object that maps each result of fn to the items that gave it
deDupe()the items with repeats removed
left(n), right(n)the first or last n items
trim(a, b)the items from position a to b, both included; negative counts from the end
clone()an independent deep copy
join(sep)the items as text, separated by sep
getKeys(name)the field name of each item
toEntriesObj()an object built from [key, value] pairs
toSet()a set of the items
iter()a lazy iterator over the items
main.osl
int[] a = [1, 2, 3, 4, 5, 6]
log a.map(n -> n * n)
log a.filter(n -> n % 2 == 0)
log [[1, 2], [3]].flatten()
log a.chunk(4)
log a.zip(["a", "b", "c"])
log a.groupBy(n -> n % 3)
log [1, 1, 2, 2, 3].deDupe()
log a.left(2) a.right(2) a.trim(2, 4) a.trim(-2, -1)
log a.join("-")
log [{name: "Ada"}, {name: "Lin"}].getKeys("name")
log [["a", 1], ["b", 2]].toEntriesObj()
log a.iter().map(n -> n + 1).take(3).toArray()
Click the code to edit it

groupBy keys are text, since they become object keys.

Sorting#

MethodGives
sort()the items in ascending orderin place
sort(fn)ordered by a comparison fn(a, b) that returns a negative, zero or positive numberin place
sortBy(key)ordered by a field name or by fn(item)in place
sortBy(key, "descending")the same, largest firstin place
reverse()the items in reverse orderin place
toSorted(), toReversed(), toSortedBy(key)the same as above, on a copycopy
randomOf()one random item

sort orders numbers by value and other items by their text. sortBy("field") puts items without that field last.

main.osl
int[] a = [5, 3, 8, 1]
log a.toSorted() a.toReversed()
log a
a.sort()
log a
a.sort(def(int x, int y) int -> y - x)
log a
string[] words = ["pear", "fig", "banana"]
log words.sortBy(w -> w.len)
log words.toSortedBy(w -> w)
object[] people = [{name: "Ada", age: 36}, {name: "Lin", age: 54}]
log people.sortBy("age", "descending").getKeys("name")
Click the code to edit it

Numbers in an array#

These need an array of numbers.

MethodGives
sum()the total (0 for an empty array)
product()the items multiplied together (1 for an empty array)
min(), max()the smallest or largest item
average()the mean
minBy(fn), maxBy(fn)the item for which fn(item) is smallest or largest
main.osl
number[] a = [4, 8, 15]
log a.sum() a.product() a.min() a.max() a.average()
object[] people = [{name: "Ada", age: 36}, {name: "Lin", age: 54}]
log people.minBy(p -> p.age).name
log people.maxBy(p -> p.age).name
Click the code to edit it

Fixed-size arrays#

int[3] is an array whose length is part of its type. Its items can be read and changed one by one, but append, prepend, pop, shift and resize are errors on it.

main.osl
int[3] fixed = [1, 2, 3]
fixed.append(4)
Click the code to edit it