lloyAlloy on GitHub
ReadArray
Docs

ReadArray

Updated
ReadArray<T>
local function total(xs: read number[]): number
    return xs:reduce(function(acc, x) return acc + x end, 0)
end

The array a read T[] names: every reader of Array<T> and none of its writers. An Array<T> passes where one is asked, so a function that only reads takes this type and says so.

Methods

ReadArray:len

ReadArray:len(): number

The number of items.

local xs: read number[] = [ 1, 2, 3 ]
print(xs:len())

ReadArray:is_empty

ReadArray:is_empty(): boolean

Whether the array holds no items.

local xs: read number[] = [ ]
print(xs:is_empty())

ReadArray:first

ReadArray:first(): T?

The first item, or nil when the array is empty.

local xs: read number[] = [ 1, 2 ]
print(xs:first())

ReadArray:last

ReadArray:last(): T?

The last item, or nil when the array is empty.

local xs: read number[] = [ 1, 2 ]
print(xs:last())

ReadArray:map

ReadArray:map<U>(f: (T, number) -> U): U[]

A new array of f(item, index) for each item. The result is a writable Array<U>.

local xs: read number[] = [ 1, 2 ]
print(xs:map(function(x) return x * 2 end):len())

ReadArray:filter

ReadArray:filter(f: (T, number) -> boolean): T[]

A new array of the items where f(item, index) holds.

local xs: read number[] = [ 1, 2 ]
print(xs:filter(function(x) return x > 1 end):len())

ReadArray:find

ReadArray:find(f: (T, number) -> boolean): T?

The first item where f(item, index) holds, or nil.

local xs: read number[] = [ 1, 2 ]
print(xs:find(function(x) return x > 1 end))

ReadArray:find_index

ReadArray:find_index(f: (T, number) -> boolean): number?

The index of the first item where f(item, index) holds, or nil.

local xs: read number[] = [ 1, 2 ]
print(xs:find_index(function(x) return x > 1 end))

ReadArray:contains

ReadArray:contains(value: T): boolean

Whether an item equals value.

local xs: read number[] = [ 1, 2 ]
print(xs:contains(2))

ReadArray:index_of

ReadArray:index_of(value: T): number?

The index of the first item that equals value, or nil.

local xs: read number[] = [ 1, 2 ]
print(xs:index_of(2))

ReadArray:for_each

ReadArray:for_each(f: (T, number) -> ())

Calls f(item, index) for each item, in order.

local xs: read number[] = [ 1, 2 ]
xs:for_each(function(x) print(x) end)

ReadArray:reduce

ReadArray:reduce<U>(f: (U, T, number) -> U, init: U): U

Folds the array from init. This is how a function that takes a read T[] sums it.

local function total(xs: read number[]): number
    return xs:reduce(function(acc: number, x) return acc + x end, 0)
end
print(total([ 1, 2 ]))

ReadArray:slice

ReadArray:slice(from: number, to: number?): T[]

A new array of the items from from to to, both inclusive. The result is writable.

local xs: read number[] = [ 1, 2, 3 ]
print(xs:slice(2):len())

ReadArray:concat

ReadArray:concat(other: T[]): T[]

A new array of this one's items followed by other's.

local xs: read number[] = [ 1 ]
print(xs:concat([ 2 ]):len())

ReadArray:join

ReadArray:join(sep: string?): string

Every item through tostring, joined by sep.

local xs: read number[] = [ 1, 2 ]
print(xs:join(", "))