Learn
Lesson 5

Arrays

Keep lists of values, read and change them by position, and transform them with map, filter and friends.

An array is an ordered list of values. Write the element type followed by [], and the items in square brackets:

main.osl
string[] planets = ["Mercury", "Venus", "Earth", "Mars"]
log planets
log planets.len
Click the code to edit it

.len is the number of items. It's a property, so it has no brackets.

Counting from one#

The first item is at position 1. Negative positions count back from the end, so the last item is always [-1]:

main.osl
string[] planets = ["Mercury", "Venus", "Earth", "Mars"]
log planets[1]
log planets[3]
log planets[-1]
planets[3] = "Home"
log planets
Click the code to edit it

There's no position 0, and the compiler says so when it sees one:

main.osl
string[] planets = ["Mercury", "Venus"]
log planets[0]
Click the code to edit it

Adding and removing#

main.osl
int[] queue = [2, 3]
queue.append(4)
queue.prepend(1)
log queue

int last = queue.pop()
int first = queue.shift()
log last first queue

queue.insert(2, 99)
log queue
queue.delete(2)
log queue
Click the code to edit it

append adds to the end and prepend to the start. pop removes and returns the last item, shift the first. insert(position, value) puts a value in at a position, and delete(position) takes one out.

Looking inside#

main.osl
string[] fruit = ["apple", "pear", "plum"]
log fruit.contains("pear")
log fruit.index("plum")
log fruit.index("kiwi")
log fruit.first() fruit.last()
Click the code to edit it

Like strings, index returns 0 when the value isn't there.

Ranges#

to makes an array of whole numbers, counting up or down, with both ends included:

main.osl
log 1 to 5
log 5 to 1
int[] evens = (1 to 10).filter(n -> n % 2 == 0)
log evens
Click the code to edit it

Transforming arrays#

The most useful array methods take a small function and apply it to every item. n -> n * 2 is a function that takes n and returns n * 2; the lambdas lesson covers them properly.

main.osl
int[] prices = [12, 5, 30, 8]

log prices.map(p -> p * 2)
log prices.filter(p -> p < 10)
log prices.some(p -> p > 25)
log prices.every(p -> p > 1)
log prices.sum() prices.min() prices.max()
Click the code to edit it
  • map makes a new array from what the function returns for each item.
  • filter keeps the items the function says true for.
  • some asks whether any item passes, and every whether all of them do.
  • sum, min and max do what they say.

They chain, reading left to right like a sentence:

main.osl
string[] names = ["ada", "grace", "al", "linus"]
log names.filter(n -> n.len > 2).map(n -> n.toUpper()).join(", ")
Click the code to edit it

Sorting#

sort returns a sorted copy: numbers in numeric order, everything else alphabetically. reverse flips the order:

main.osl
int[] scores = [42, 7, 19, 88]
log scores.sort()
log scores.sort().reverse()
log ["pear", "apple", "fig"].sort()
Click the code to edit it

Joining and spreading#

++ joins two arrays, and ... spreads one array's items into another:

main.osl
int[] low = [1, 2]
int[] high = [8, 9]
log low ++ high
log [...low, 5, ...high]
log max(...high)
Click the code to edit it

Taking arrays apart#

Destructuring puts an array's items straight into variables. ...rest collects whatever is left:

main.osl
string[] podium = ["Ada", "Grace", "Linus", "Margaret"]
[string gold, string silver, ...others] = podium
log gold
log silver
log others
Click the code to edit it

Sharing and copying#

An array is shared, not copied, when you assign it to another name. Both names then see the same list:

main.osl
int[] a = [1, 2, 3]
int[] b = a
b.append(4)
log a

int[] c = a.clone()
c.append(5)
log a
log c
Click the code to edit it

Use .clone() when you want a separate copy to change.

Arrays of anything#

int[] and string[] hold one type each, which is what you want most of the time. Arrays can hold arrays, as in int[][] for a grid, and plain array holds a mix of types:

main.osl
int[][] grid = [[1, 2], [3, 4]]
log grid[2][1]

array mixed = [1, "two", true]
log mixed.len
Click the code to edit it
Quick check

int[] n = [10, 20, 30]. What is n[-1]?

Quick check

What does [3, 1, 2].filter(x -> x > 1).sort() give?

Your turn

Top three

Print the three highest scores, highest first, joined with commas.

main.osl
Ctrl Enter runs
Expected output
99, 91, 88
0 of 24 lessons done