Module MList

This module is undocumented. This is a list of its definitions.

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

Adds the given element to the end of the list.

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

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

clear :: MList.T a -> <Proc> ()

Removes all elements from the list.

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

Returns True if the list contains the given value.

create :: () -> <Proc> MList.T a

Creates a new empty list.

createC :: Integer -> <Proc> MList.T a

Creates a new empty list with room for the given number of elements.

first :: MList.T a -> <Proc> a

Returns the first element of a list.

Fails with an IndexOutOfBoundsException if the list is empty; there is no Maybe in the result type to signal that. Check isEmpty first, or use removeLast if consuming the list is acceptable.

Example:

> l = MList.fromList [10, 20, 30]
> MList.first l
10
fold :: (a -> b -> <c> a) -> a -> MList.T b -> <Proc,c> a

Folds over all elements of the list starting with the given initial value.

freeze :: MList.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. When both are needed, use toList, which copies.

Example:

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

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

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

get l i returns the element of l at the index i. Indexing starts from zero.

isEmpty :: MList.T a -> <Proc> Boolean

Returns True if the list contains no elements.

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

Calls the given function with all elements of the list.

iterB :: (a -> <b> Boolean) -> MList.T a -> <b,Proc> Boolean

Calls the given function with the elements of the list until it returns False. Returns False if the iteration was interrupted this way and True if the function accepted all elements.

The result means "was not interrupted", not "found something". See MMap.iterB for a worked example of the same pattern.

last :: MList.T a -> <Proc> a

Returns the last element of a list.

Like first, this fails with an IndexOutOfBoundsException on an empty list.

Example:

> l = MList.fromList [10, 20, 30]
> MList.last l
30
remove :: MList.T a -> Integer -> <Proc> a

Removes the element at the given index and returns it. Indexing starts from zero.

The argument is an index, not a value to look for. For a list of integers this is easy to get wrong, since MList.remove l 2 removes the third element rather than the element 2.

Example:

> l = MList.fromList [10, 20, 30]
> MList.remove l 2
30
> MList.toList l
[10, 20]
removeLast :: MList.T a -> <Proc> Maybe a

Removes the last element of the list and returns it, or returns Nothing if the list is empty. Unlike remove, this is safe on an empty list.

Example:

> l = MList.fromList [1, 2, 3]
> MList.removeLast l
Just 3
> MList.toList l
[1, 2]
set :: MList.T a -> Integer -> a -> <Proc> Maybe a

set l i v replaces the element at the index i by v and returns the element that was there before. Indexing starts from zero.

The previous element comes back wrapped in Maybe. (ArrayList.set does the same thing but returns a plain a; the two modules are inconsistent here.)

Example:

> l = MList.fromList ["a", "b", "c"]
> MList.set l 1 "B"
Just "b"
> MList.toList l
["a", "B", "c"]
singleton :: a -> <Proc> MList.T a

Creates a new mutable list containing just the given element.

size :: MList.T a -> <Proc> Integer

The number of elements in the list.

toList :: MList.T a -> <Proc> [a]

Creates an independent copy of the list as an immutable list. Unlike freeze, this does not invalidate the original mutable list, which can still be used afterwards.

This is the O(n) alternative to freeze. Use it whenever the mutable list is still needed after the conversion.

Example:

> l = MList.fromList [1, 2, 3]
> copy = MList.toList l
> MList.add l 4
> copy
[1, 2, 3]
> MList.toList l
[1, 2, 3, 4]