Lists
List is the ordered, indexable sequence behind the literal [1, 2, 3], with ArrayList as the default implementation and a verb paired with a participle for every change.
Diese Seite gibt es noch nicht auf Deutsch, deshalb steht sie hier auf Englisch. Zur englischen Seite
List<Item> is an ordered sequence, addressable by index, and the type of the literal [1, 2, 3]. Every change has a
verb that changes the list in place and a participle that answers a changed copy, so the same operation is available
whether the binding is var or const.
Example#
var numbers = [3, 1, 2]
numbers.append 4
numbers[0] = 5
const doubled = numbers.appended(6).sorted { value => value }.map { _ * 2 }.toList()
print numbers
print doubledSyntax#
[1, 2, 3] the literal: ArrayList<Int>
List.of(1, 2, 3) from arguments
List.of(...anyIterate) from a spread
List.from(anyIterate) from any Iterate
anyIterate.toList() from a pipeline
list[index] Index.at: panics if index is out of range
list.get(index) Index.get: an Option
list[index] = value MutableIndex.setRules#
A verb changes the list in place through a
varpath, and its participle answers a changed copy and works on aconstlist too.append/appended,insert/inserted,remove/removed,removeAt/removedAt,sort/sorted,reverse/reversed. A participle is built from its verb once, as a default method ofList:appendedcopies the list into avar, callsappendon the copy and answers it.Reading
list[index]panics if the index is out of range;list.get(index)answers anOptioninstead.[index]isIndex.at, which callsgetand panics onNone;getitself never panics.const numbers = [1, 2, 3] print numbers.get(10) print numbers[0]Equals,HashandShoware generated whereverItemsupports them, and aListis order-dependent in all three. Two lists with the same items in a different order are unequal, hash differently, and print differently - unlike Maps and sets, which ignore insertion order for both.Creation goes through
List.of,List.from, a literal, ortoList()on anyIterate, and every one of them answers the default implementation,ArrayList.List.of(...items)accepts a spread of anyIterate, and a nested list needs its own brackets:List.of([1, 2], [3])is aList<List<Int>>, because a variadic parameter never unpacks by itself.first(),last()andindexOf(value)read without changing anything, and answer anOption:first()andlast()areNonefor an empty list,indexOfisNonewhen the value is not present.ArrayListis the default and is contiguous;TrieListis a bit-partitioned trie whose write to a shared list copies one path instead of the whole buffer. Until the trie is implemented,TrieListis a documented alias ofArrayList: only performance differs, iteration order andShoware the same either way.
What this is not#
A List has no +. Combining two lists is appendedAll, a word that says which end the second list goes to,
not an operator.
const numbers = [1, 2, 3]
const combined = numbers.appendedAll([4, 5])
print combinedconst numbers = [1, 2, 3]
const combined = numbers + [4, 5]
// error: `List<Int64>` does not implement `Add`, so `a + b` has no meaning for itRelated#
- The collection traits -
IterateandLength, whichListis built on. - Slices -
list[from..to], aListagain, as a value and as avarpath. - Verbs and participles - the naming rule behind
append/appendedand its siblings. - Mutation and var paths - why a verb needs a
varpath and a participle does not.