1 import "Prelude" hiding (length, contains, iter, for)
2 import "Prelude" as Prelude
3 import "JavaBuiltin" as Java
5 importJava "java.util.ArrayList" where
9 "Constructs a new list."
11 new :: () -> <Proc> (T a)
13 "Constructs a new list with initial capacity."
15 newC :: Integer -> <Proc> (T a)
17 "Adds an element to the list."
18 add :: T a -> a -> <Proc> ()
20 addAll :: T a -> [a] -> <Proc> ()
22 "Gets the i:th element of the list."
23 get :: T a -> Integer -> <Proc> a
25 "Sets the i:th element of the list."
26 set :: T a -> Integer -> a -> <Proc> a
28 "Removes the i:th element of the list"
29 remove :: T a -> Integer -> <Proc> a
31 "The current length of the list."
33 length :: T a -> <Proc> Integer
35 contains :: T a -> a -> <Proc> Boolean
45 "Converts the mutable list into immutable list. The original list must not be modified anymore."
46 freeze :: T a -> <Proc> [a]
47 freeze = Java.unsafeCoerce
49 "Iterates thru the list. The elements added during the iteration are also iterated."
50 iter :: (a -> <e> ()) -> T a -> <Proc,e> ()
53 loop i = if i >= length l
55 else do f (get l i) ; loop (i+1)
57 "'iter' with swapped parameter."
58 for :: T a -> (a -> <e> ()) -> <Proc,e> ()
61 "Replaces every element of the list by the result of applying the element to the given function."
62 mapInPlace :: (a -> <e> a) -> T a -> <Proc,e> T a
63 mapInPlace f l = loop 0
68 else do set l i (f (get l i))
71 "Pops the last element of the list until list becomes empty."
72 popUntilEmpty :: T a -> (a -> <e> ()) -> <Proc,e> ()
73 popUntilEmpty l f = do
77 else do f (remove l (len-1))
80 fromList :: [a] -> <Proc> T a
82 result = newC (Prelude.length l)