Basic methods

data T a

Type of lists.

new :: () -> <Proc> ArrayList.T a

Constructs a new list.

add :: ArrayList.T a -> a -> <Proc> ()

Adds an element to the list.

remove :: ArrayList.T a -> Integer -> <Proc> a

Removes the i:th element of the list and returns it. Indexing starts from zero.

get :: ArrayList.T a -> Integer -> <Proc> a

Gets the i:th element of the list. Indexing starts from zero.

length :: ArrayList.T a -> <Proc> Integer

The current length of the list.

contains :: ArrayList.T a -> a -> <Proc> Boolean

Returns True if the list contains the given element.

Iteration

iter :: (a -> <b> ()) -> ArrayList.T a -> <Proc,b> ()

Iterates through the list. The elements added during the iteration are also iterated.

The length is re-read before every step, so appending to the list from inside the function extends the iteration instead of failing or being ignored. That makes this usable as a worklist loop, and also means the iteration does not terminate if the function always appends. mapInPlace behaves differently: it reads the length once up front.

Example:

> l = ArrayList.fromList [1, 2]
> ArrayList.iter (\x -> if x < 4 then ArrayList.add l (x + 2) else ()) l
> ArrayList.freeze l
[1, 2, 3, 4, 5]

The 3 and 4 appended while iterating 1 and 2 were themselves visited, which is how 5 came to be added.

for :: ArrayList.T a -> (a -> <b> ()) -> <Proc,b> ()

iter with the parameters swapped: the list comes first. See iter for the semantics.

mapInPlace :: (a -> <b> a) -> ArrayList.T a -> <Proc,b> ArrayList.T a

Replaces every element of the list by the result of applying the element to the given function.

The given list is mutated and the very same list is returned; the result is not a copy, so the return value can be ignored.

Unlike iter, the length is read once before the loop starts. Elements appended during the traversal are therefore not visited.

Examples:

> l = ArrayList.fromList [1, 2, 3]
> ArrayList.freeze (ArrayList.mapInPlace (\x -> x * 10) l)
[10, 20, 30]

Only the two original elements are mapped here, even though the list grows to four:

> l = ArrayList.fromList [1, 2]
> ArrayList.freeze (ArrayList.mapInPlace (\x -> do ArrayList.add l 99 ; x * 10) l)
[10, 20, 99, 99]
popUntilEmpty :: ArrayList.T a -> (a -> <b> ()) -> <Proc,b> ()

Pops the last element of the list until list becomes empty.

Elements are consumed from the end of the list, so this is a LIFO worklist drain, not a front-to-back traversal: the function is called with the elements in reverse order and the list is left empty. Because the last element is re-read on every step, the function may push more work onto the list.

Note the argument order, which is the reverse of iter: the list comes first and the function second, like for.

Example:

> l = ArrayList.fromList [1, 2, 3]
> ArrayList.popUntilEmpty l (\x -> print x)
3
2
1
> ArrayList.length l
0

Undocumented entities

addAll :: ArrayList.T a -> [a] -> <Proc> ()

Adds all elements of the given list to the end of the list.

freeze :: ArrayList.T a -> <Proc> [a]

Converts the mutable list into an immutable one without copying the underlying data. This only changes how the structure is treated by the SCL type system; it is an O(1) operation, not a copy. The original mutable list must not be used anymore after this.

The result aliases the original: it is the same object, only retyped. Every subsequent mutation of the mutable list is visible through the supposedly immutable one, as the example shows. That is why the original must be dropped. This module has no copying counterpart; MList.toList is the one to reach for when both a mutable and an independent immutable list are needed.

Example:

> l = ArrayList.fromList [1, 2]
> frozen = ArrayList.freeze l
> ArrayList.add l 3
> frozen
[1, 2, 3]
fromList :: [a] -> <Proc> ArrayList.T a

Creates a new mutable list containing the elements of the given immutable list.

newC :: Integer -> <Proc> ArrayList.T a

Constructs a new list with initial capacity.

set :: ArrayList.T a -> Integer -> a -> <Proc> a

Sets the i:th element of the list and returns the element that was there before. Indexing starts from zero.

(MList.set does the same thing but wraps the previous element in Maybe; the two modules are inconsistent here.)

Example:

> l = ArrayList.fromList ["a", "b"]
> ArrayList.set l 0 "A"
"a"
> ArrayList.freeze l
["A", "b"]