next up previous contents index
Next: Tables (mappings) Up: Collections Previous: Ordered collections and sequences

Adding and removing elements

In extensible.cecil:


abstract object removable_collection[T] isa collection[T]; 
signature remove(removable_collection[`T], x:T, if_absent:&():void):void; 
method remove(c@:removable_collection[`T], x:T):void; 
method remove_if(c@:removable_collection[`T], pred:&(T):bool):int; 
signature remove_any(removable_collection[`T], if_empty:&():`S):T|S; 
method remove_any(c@:removable_collection[`T]):T; 
method remove_all(c@:removable_collection[`T]):void; 
A removable_collection supports in-place removal of elements. The remove function removes one element equal to its second argument, or raises an error if not found (also defined is a variation that takes a closure to handle the not-found case). The remove_if function removes all elements that satisfy the test, and returns the number of elements removed. The remove_any function removes and returns a single arbitrary element of the collection or raises an error if the collection is empty (again, a variation on remove_any passes a closure to handle this case in a client-defined manner). The remove_all function removes all elements of the collection. Subclasses need to implement these functions; a default remove_all is provided, but it is likely that subclasses can provide a much more efficient implementation.


abstract object functionally_extensible_collection[T] isa collection[T]; 
signature add_functional(functionally_extensible_collection[`T], x:T 
):functionally_extensible_collection[T];
The functionally_extensible_collection class generalizes collections that are extensible in place (i.e., add mutates) and those that are extensible, but not in place (i.e., add returns a new collection). The add_functional operation adds an element to the collection (in some unspecified location), returning a collection with the element added. This returned collection may either be the receiver collection (if the add is performed in-place) or some new collection. Since the receiver collection may or may not be changed and may or may not contain the new element, the caller should use the returned value (and should not use the argument to add_functional after the call).

The weird name of the collection is intended to suggest a functional language where add returns a new collection.


abstract object functionally_extensible_removable_collection[T] 
isa removable_collection[T], functionally_extensible_collection[T];
signature add_functional(functionally_extensible_removable_collection[`T],x:T 
):functionally_extensible_removable_collection[T];
signature copy(functionally_extensible_removable_collection[`T] 
):functionally_extensible_removable_collection[T];
abstract object extensible_collection[T] 
isa functionally_extensible_removable_collection[T];
signature add(extensible_collection[`T], x:T):void; 
method add_nonmember(c@:extensible_collection[`T], x:T):void; 
method add_all(c@:extensible_collection[`T], xs:collection[T]):void; 
method add_all_nonmember(c@:extensible_collection[`T], xs:collection[T]):void; 
method add_functional(m@:extensible_collection[`T], x:T 
):extensible_collection[T];
A blend of functionally_extensible_collection and removable_collection.


abstract object extensible_ordered[T] 
isa extensible_collection[T], ordered_collection[T];
signature remove_first(extensible_ordered[`T], if_empty:&():`S):T|S; 
method remove_first(c@:extensible_ordered[`T]):T; 
signature remove_last(extensible_ordered[`T], if_empty:&():`S):T|S; 
method remove_last(c@:extensible_ordered[`T]):T; 
method remove_any(c@:extensible_ordered[`T], if_empty:&():`S):T|S; 
Extensible ordered collections can have their first and last elements removed, as well as the removing and adding behavior of extensible collections.


abstract object extensible_sequence[T] isa extensible_ordered[T], sequence[T]; 
signature add_first(extensible_sequence[`T], x:T):void; 
signature add_last (extensible_sequence[`T], x:T):void; 
method add(c@:extensible_sequence[`T], x:T):void; 
method add_all_last(s@:extensible_sequence[`T], c@:ordered_collection[T]):void; 
Extensible sequences also allow elements to be added to the front or end of the sequence.


next up previous contents index
Next: Tables (mappings) Up: Collections Previous: Ordered collections and sequences

The Cecil project