User Tools

Site Tools


at:tutorial:appendix

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
at:tutorial:appendix [2008/07/10 14:31] – * tvcutsemat:tutorial:appendix [2008/07/10 15:05] – * tvcutsem
Line 102: Line 102:
 ==== Vector ==== ==== Vector ====
  
-A vector is a dynamically resizable AmbientTalk table (aka array). Vectors may be created as follows:+A vector is a dynamically resizable AmbientTalk table (aka array). Indexed reading from and writing to a vector is fast (O(1)). Adding elements to a vector is mostly fast, but sometimes requires a resize of the vectorVectors support the traditional stack operations ''push'' and ''pop'' and may be turned into sets by invoking their ''uniq'' method (note that a ''uniq''-ed vector is not permanently a Set: subsequent duplicates added to the vector will not be filtered). 
 + 
 +Vectors may be created as follows:
  
 <code> <code>
Line 125: Line 127:
  
 // write idx'th element or raise IndexOutOfBounds exception // write idx'th element or raise IndexOutOfBounds exception
-//atPut(idx, val)+atPut(idx, val)
  
 // iterate over the vector // iterate over the vector
Line 148: Line 150:
 select(start, stop) select(start, stop)
  
-// appends an element to the back of the vector +// appends an element to the back of the vector. Returns the vector itself
-// returns the vector itself+
 add(element) add(element)
 // alias for add(element) // alias for add(element)
Line 203: Line 204:
 asTable() asTable()
 </code> </code>
 +
 +The file ''at/collections/vector.at'' contains a unit tests that further helps to illustrate the usage of this Vector abstraction.
  
 ==== List ==== ==== List ====
 +
 +The module ''/.at.collections.list'' implements Scheme-like list datastructures. The module exports the prototype ''NIL'', which is bound to the empty list. Non-empty lists are defined as a chain of cons-cells.
 +
 +The list module defines two styles to manipulate cons-cells: an object-oriented and a functional style. The object-oriented style represents cons-cells as ''Cons'' prototypes. Given a cons-cell ''c'', a new one can be constructed by invoking ''c.new(car, cdr)''. The car and cdr part of the cons-cell can be extracted by means of ''c.car'' and ''c.cdr''.
 +
 +The functional style allows one to manipulate lists by means of the following functions:
 +
 +<code>
 +cons(car,cdr) -> a new cons-cell
 +car(conscell) -> the car
 +cdr(conscell) -> the cdr
 +list(@items) -> a cons-cell representing the head of a list
 +</code>
 +
 +Lists (cons-cells or the empty list) support the following operations:
 +
 +<code>
 +// accessors for car and cdr
 +car()
 +cdr()
 +
 +// the length of the list
 +length()
 +
 +// whether the list is empty or not
 +isEmpty()
 +
 +// returns the nth element of the list
 +nth(n)
 +
 +// apply a unary function to each element of the list
 +each: fun
 +
 +// apply a function to each element and its index in the list
 +// i.e. list.eachWithIndex: { |elt, idx| ... }
 +eachWithIndex: fun
 +
 +// map a unary function over the list, returning a new list
 + map: fun
 +
 +// accumulate a value over a list
 +inject: init into: accum
 +
 +// return a new list whose elements satisfy the unary predicate
 +filter: cond
 +
 +// does the list contain the element?
 +contains(elt, cmp := DEFAULTCOMPARATOR)
 +
 +// implode or join a list of text strings
 +implode()
 +join(txt)
 +
 +// drop the first n elements from the list
 +tail(n)
 +
 +// prepend an element to the list
 +add(elt)
 +
 +// insert an element in the list (functionally)
 +insert(atPos, element)
 +
 +// return a new list where the element atPos is deleted
 +delete(atPos)
 +
 +// functional append
 +append(aList)
 +
 +// return the index of the first matching element, or nil if none is found
 +find: filter
 +
 +// return the index in the list of the element or nil of not found
 +indexOf(elt, cmp := DEFAULTCOMPARATOR)
 +
 +// return a list where the given element is removed
 +remove(elt, cmp := DEFAULTCOMPARATOR)
 +
 +// return a new list where all objects for which filter(elt) is true are removed
 +removeAll: filter
 +
 +// convert the list into a table
 +asTable()
 +</code>
 +
 +The file ''at/collections/list.at'' contains a unit test that further illustrates the usage of the list datastructure.
at/tutorial/appendix.txt · Last modified: 2021/09/24 10:28 by elisag