Ink by Example: Lists

Lists and maps share the same data structure — the composite value. At runtime, lists are hashmaps with string keys representing indices.

std := load('../vendor/std')
log := std.log
reverse := std.reverse
slice := std.slice
range := std.range
each := std.each
join := std.join


Lists can be declared with array syntax.

names := ['Alice', 'Andrew']
log(names)

To access or mutate an element we use the dot syntax.

log(names.0) `` first element
log(names.(len(names) - 1)) `` last element

names.0 := 'Madeline'
log(names)

Like strings, we can append to the end of a list by mutating the next index value.

names.len(names) := 'Francisca'
log(names)

indexer := (name, index) => (
    log(index)
    log(name)
)
each(names, indexer)

std.range works similar to Python's range. It takes start, end, and step values and returns a list.

numbers := range(0, 6, 1)
log(numbers)

We can get a sublist with std.slice, and join two lists to create a third with std.join.

half := slice(numbers, 0, 3)
log(half)

first := [98, 99]
second := [100, 101]
third := join(first, second)
log(third)

To reverse, use std.reverse. For other list utilities, refer to the standard library.

log(reverse(['one', 'two', 'three', ]))

$ ink lists.ink
{0: 'Alice', 1: 'Andrew'}
Alice
Andrew
{0: 'Madeline', 1: 'Andrew'}
{2: 'Francisca', 0: 'Madeline', 1: 'Andrew'}
0
Madeline
1
Andrew
2
Francisca
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
{0: 0, 1: 1, 2: 2}
{0: 98, 1: 99, 2: 100, 3: 101}
{2: 'one', 0: 'three', 1: 'two'}

Next example: Maps.