1  package frost.collections
 2  
 3  ====================================================================================================
 4  A write-only view of a collection of elements. `CollectionWriter` provides methods to add and
 5  remove elements, but not to retrieve them.
 6  
 7  @see CollectionView
 8  @see Collection
 9  @see ListWriter
10  ====================================================================================================
11  interface CollectionWriter<T> {
12      ================================================================================================
13      The number of elements in the collection.
14      ================================================================================================
15      property count:Int
16  
17      function get_count():Int
18  
19      ================================================================================================
20      Adds a new element to the collection. The exact semantics of `add` - does it add to the end of
21      the collection, or an arbitrary location? does it always actually add the element, or sometimes
22      leave the collection unmodified? - are defined by the collection implementation.
23      ================================================================================================
24      method add(value:T)
25  
26      ================================================================================================
27      Adds all elements in `c` to this collection. The default implementation simply calls [add] for
28      each element in `c`, in iteration order.
29      ================================================================================================
30      @default
31      method addAll(c:CollectionView<T>) {
32          for v in c {
33              add(v)
34          }
35      }
36  
37      ================================================================================================
38      Removes all elements in the collection.
39      ================================================================================================
40      @post(count = 0)
41      method clear()
42  
43      ================================================================================================
44      Calls the `test` function on each element in the collection, removing all elements for which the
45      function returns `false`.
46  
47      For example, the code
48  
49          -- testcase CollectionWriterFilterInPlace(Simple)
50          def collection := [1, 2, 3, 4 ,5]
51          collection.filterInPlace(x => x % 2 = 1)
52          Console.printLine(collection)
53  
54      will display `[1, 3, 5]`, as the filter function returns true only for elements with odd values.
55      ================================================================================================
56      method filterInPlace(test:(T)=>(Bit))
57  
58      method mapInPlace(f:(T)=>(T))
59  }
60