# LinearAggregate S¶

aggcat.spad line 1698 [edit on github]

S: Type

A linear aggregate is an aggregate whose elements are indexed by integers. Examples of linear aggregates are strings, lists, and arrays. Most of the exported operations for linear aggregates are non-destructive but are not always efficient for a particular aggregate. For example, concat of two lists needs only to copy its first argument, whereas concat of two arrays needs to copy both arguments. Most of the operations exported here apply to infinite objects (e.g. streams) as well to finite ones. If the aggregate is a finite aggregate then it has several additional exports such as reverse, sort, and so on.

- #: % -> NonNegativeInteger if % has finiteAggregate
from Aggregate

- <=: (%, %) -> Boolean if S has OrderedSet and % has finiteAggregate
from PartialOrder

- <: (%, %) -> Boolean if S has OrderedSet and % has finiteAggregate
from PartialOrder

- =: (%, %) -> Boolean if S has BasicType and % has finiteAggregate or S has Hashable and % has finiteAggregate or S has SetCategory
from BasicType

- >=: (%, %) -> Boolean if S has OrderedSet and % has finiteAggregate
from PartialOrder

- >: (%, %) -> Boolean if S has OrderedSet and % has finiteAggregate
from PartialOrder

- ~=: (%, %) -> Boolean if S has BasicType and % has finiteAggregate or S has Hashable and % has finiteAggregate or S has SetCategory
from BasicType

- any?: (S -> Boolean, %) -> Boolean if % has finiteAggregate
from HomogeneousAggregate S

- coerce: % -> OutputForm if S has CoercibleTo OutputForm
from CoercibleTo OutputForm

- concat: (%, %) -> %
`concat(u, v)`

returns an aggregate consisting of the elements of`u`

followed by the elements of`v`

. Note: if`w = concat(u, v)`

then`w.i = u.i for i in indices u`

and`w.(j + maxIndex u) = v.j for j in indices v`

.

- concat: (%, S) -> %
`concat(u, x)`

returns aggregate`u`

with additional element`x`

at the end. Note: for lists,`concat(u, x) = concat(u, [x])`

.

- concat: (S, %) -> %
`concat(x, u)`

returns aggregate`u`

with additional element`x`

at the front. Note: for lists:`concat(x, u) = concat([x], u)`

.

- concat: List % -> %
`concat(u)`

, where`u`

is a list of aggregates`[a, b, ..., c]`

, returns a single aggregate consisting of the elements of`a`

followed by those of`b`

followed … by the elements of`c`

. Note:`concat([a, b, ..., c]) = concat(a, concat([b, ..., c]))`

.

- construct: List S -> %
from Collection S

- convert: % -> InputForm if S has ConvertibleTo InputForm
from ConvertibleTo InputForm

- copyInto!: (%, %, Integer) -> % if % has finiteAggregate and % has shallowlyMutable
`copyInto!(u, v, i)`

returns aggregate`u`

containing a copy of`v`

inserted at element`i`

.

- count: (S -> Boolean, %) -> NonNegativeInteger if % has finiteAggregate
from HomogeneousAggregate S

- count: (S, %) -> NonNegativeInteger if S has BasicType and % has finiteAggregate
from HomogeneousAggregate S

- delete: (%, Integer) -> %
`delete(u, i)`

returns a copy of`u`

with the`i`

th element deleted. Note:`delete(u, i) = concat(u(minIndex(u)..i - 1), u(i + 1..))`

.

- delete: (%, UniversalSegment Integer) -> %
`delete(u, i..j)`

returns a copy of`u`

with the`i`

th through`j`

th element deleted. Note:`delete(u, i..j) = concat(u(minIndex(u)..i-1), u(j+1..))`

.

- elt: (%, Integer) -> S
- elt: (%, Integer, S) -> S
from EltableAggregate(Integer, S)

- elt: (%, UniversalSegment Integer) -> %
from Eltable(UniversalSegment Integer, %)

- entries: % -> List S
from IndexedAggregate(Integer, S)

- entry?: (S, %) -> Boolean if S has BasicType and % has finiteAggregate
from IndexedAggregate(Integer, S)

- eval: (%, Equation S) -> % if S has Evalable S and S has SetCategory
from Evalable S

- eval: (%, List Equation S) -> % if S has Evalable S and S has SetCategory
from Evalable S

- eval: (%, List S, List S) -> % if S has Evalable S and S has SetCategory
from InnerEvalable(S, S)

- eval: (%, S, S) -> % if S has Evalable S and S has SetCategory
from InnerEvalable(S, S)

- every?: (S -> Boolean, %) -> Boolean if % has finiteAggregate
from HomogeneousAggregate S

- fill!: (%, S) -> % if % has shallowlyMutable
from IndexedAggregate(Integer, S)

- find: (S -> Boolean, %) -> Union(S, failed)
from Collection S

- first: % -> S
from IndexedAggregate(Integer, S)

- first: (%, NonNegativeInteger) -> %
`first(u, n)`

returns a copy of the first`n`

elements of`u`

. Error: if`u`

has less than`n`

elements.

- hash: % -> SingleInteger if S has Hashable and % has finiteAggregate
from Hashable

- hashUpdate!: (HashState, %) -> HashState if S has Hashable and % has finiteAggregate
from Hashable

- index?: (Integer, %) -> Boolean
from IndexedAggregate(Integer, S)

- indices: % -> List Integer
from IndexedAggregate(Integer, S)

- insert: (%, %, Integer) -> %
`insert(v, u, i)`

returns a copy of`u`

having`v`

inserted beginning at the`i`

th element. Note:`insert(v, u, i) = concat(u(minIndex(u)..i-1), concat(v, u(i..)))`

.

- insert: (S, %, Integer) -> %
`insert(x, u, i)`

returns a copy of`u`

having`x`

as its`i`

th element. Note:`insert(x, u, i) = concat(u(minIndex(u)..i-1), concat(x, u(i..)))`

.

- latex: % -> String if S has SetCategory
from SetCategory

- leftTrim: (%, S) -> % if S has BasicType and % has finiteAggregate
`leftTrim(u, x)`

returns a copy of`u`

with all leading`x`

deleted. For example,`leftTrim(" abc ", char " ")`

returns`"abc "`

.

- less?: (%, NonNegativeInteger) -> Boolean
from Aggregate

- map!: (S -> S, %) -> % if % has shallowlyMutable
from HomogeneousAggregate S

- map: ((S, S) -> S, %, %) -> %
`map(f, u, v)`

returns a new aggregate`w`

with elements`z = f(x, y)`

for corresponding elements`x`

and`y`

from`u`

and`v`

. Note:`w.i = f(u.i, v.i)`

.- map: (S -> S, %) -> %
from HomogeneousAggregate S

- max: % -> S if S has OrderedSet and % has finiteAggregate
from HomogeneousAggregate S

- max: (%, %) -> % if S has OrderedSet and % has finiteAggregate
from OrderedSet

- max: ((S, S) -> Boolean, %) -> S if % has finiteAggregate
from HomogeneousAggregate S

- maxIndex: % -> Integer
from IndexedAggregate(Integer, S)

- member?: (S, %) -> Boolean if S has BasicType and % has finiteAggregate
from HomogeneousAggregate S

- members: % -> List S if % has finiteAggregate
from HomogeneousAggregate S

- merge: (%, %) -> % if S has OrderedSet and % has finiteAggregate
`merge(u, v)`

merges`u`

and`v`

in ascending order. Note:`merge(u, v) = merge(<=, u, v)`

.

- merge: ((S, S) -> Boolean, %, %) -> % if % has finiteAggregate
`merge(p, a, b)`

returns an aggregate`c`

which merges`a`

and`b`

. The result is produced by examining each element`x`

of`a`

and`y`

of`b`

successively. If`p(x, y)`

is`true`

, then`x`

is inserted into the result; otherwise`y`

is inserted. If`x`

is chosen, the next element of`a`

is examined, and so on. When all the elements of one aggregate are examined, the remaining elements of the other are appended. For example,`merge(<, [1, 3], [2, 7, 5])`

returns`[1, 2, 3, 7, 5]`

.

- min: % -> S if S has OrderedSet and % has finiteAggregate
from HomogeneousAggregate S

- min: (%, %) -> % if S has OrderedSet and % has finiteAggregate
from OrderedSet

- minIndex: % -> Integer
from IndexedAggregate(Integer, S)

- more?: (%, NonNegativeInteger) -> Boolean
from Aggregate

- new: (NonNegativeInteger, S) -> %
`new(n, x)`

returns a new aggregate of size`n`

all of whose entries are`x`

.

- parts: % -> List S if % has finiteAggregate
from HomogeneousAggregate S

- position: (S -> Boolean, %) -> Integer if % has finiteAggregate
`position(p, a)`

returns the index`i`

of the first`x`

in`a`

such that`p(x)`

is`true`

, and`minIndex(a) - 1`

if there is no such`x`

.

- position: (S, %) -> Integer if S has BasicType and % has finiteAggregate
`position(x, a)`

returns the index`i`

of the first occurrence of`x`

in a, and`minIndex(a) - 1`

if there is no such`x`

.

- position: (S, %, Integer) -> Integer if S has BasicType and % has finiteAggregate
`position(x, a, n)`

returns the index`i`

of the first occurrence of`x`

in`a`

where`i >= n`

, and`minIndex(a) - 1`

if no such`x`

is found.

- qelt: (%, Integer) -> S
from EltableAggregate(Integer, S)

- qsetelt!: (%, Integer, S) -> S if % has shallowlyMutable
from EltableAggregate(Integer, S)

- reduce: ((S, S) -> S, %) -> S if % has finiteAggregate
from Collection S

- reduce: ((S, S) -> S, %, S) -> S if % has finiteAggregate
from Collection S

- reduce: ((S, S) -> S, %, S, S) -> S if S has BasicType and % has finiteAggregate
from Collection S

- remove: (S -> Boolean, %) -> % if % has finiteAggregate
from Collection S

- remove: (S, %) -> % if S has BasicType and % has finiteAggregate
from Collection S

- removeDuplicates: % -> % if S has BasicType and % has finiteAggregate
from Collection S

- reverse!: % -> % if % has finiteAggregate and % has shallowlyMutable
`reverse!(u)`

returns`u`

with its elements in reverse order.

- reverse: % -> % if % has finiteAggregate
`reverse(a)`

returns a copy of`a`

with elements in reverse order.

- rightTrim: (%, S) -> % if S has BasicType and % has finiteAggregate
`rightTrim(u, x)`

returns a copy of`u`

with all trailing occurrences of`x`

deleted. For example,`rightTrim(" abc ", char " ")`

returns`" abc"`

.

- select: (S -> Boolean, %) -> % if % has finiteAggregate
from Collection S

- setelt!: (%, Integer, S) -> S if % has shallowlyMutable
from EltableAggregate(Integer, S)

- setelt!: (%, UniversalSegment Integer, S) -> S if % has shallowlyMutable
`setelt!(u, i..j, x)`

(also written:`u(i..j) := x`

) destructively replaces each element in the segment`u(i..j)`

by`x`

. The value`x`

is returned. Note:`u`

is destructively changed so that`u.k := x for k in i..j`

; its length remains unchanged.

- size?: (%, NonNegativeInteger) -> Boolean
from Aggregate

- smaller?: (%, %) -> Boolean if S has OrderedSet and % has finiteAggregate or % has finiteAggregate and S has Comparable
from Comparable

- sort!: % -> % if S has OrderedSet and % has finiteAggregate and % has shallowlyMutable
`sort!(u)`

returns`u`

with its elements in ascending order.

- sort!: ((S, S) -> Boolean, %) -> % if % has finiteAggregate and % has shallowlyMutable
`sort!(p, u)`

returns`u`

with its elements ordered by`p`

.

- sort: % -> % if S has OrderedSet and % has finiteAggregate
`sort(u)`

returns an`u`

with elements in ascending order. Note:`sort(u) = sort(<, u)`

.

- sort: ((S, S) -> Boolean, %) -> % if % has finiteAggregate
`sort(p, a)`

returns a copy of`a`

sorted using total ordering predicate`p`

.

- sorted?: % -> Boolean if S has OrderedSet and % has finiteAggregate
`sorted?(u)`

tests if the elements of`u`

are in ascending order.

- sorted?: ((S, S) -> Boolean, %) -> Boolean if % has finiteAggregate
`sorted?(p, a)`

tests if`a`

is sorted according to predicate`p`

.

- swap!: (%, Integer, Integer) -> Void if % has shallowlyMutable
from IndexedAggregate(Integer, S)

- trim: (%, S) -> % if S has BasicType and % has finiteAggregate
`trim(u, x)`

returns a copy of`u`

with all occurrences of`x`

deleted from right and left ends. For example,`trim(" abc ", char " ")`

returns`"abc"`

.

BasicType if S has SetCategory or S has Hashable and % has finiteAggregate or S has BasicType and % has finiteAggregate

CoercibleTo OutputForm if S has CoercibleTo OutputForm

Comparable if S has OrderedSet and % has finiteAggregate or % has finiteAggregate and S has Comparable

ConvertibleTo InputForm if S has ConvertibleTo InputForm

Eltable(UniversalSegment Integer, %)

Evalable S if S has Evalable S and S has SetCategory

Hashable if S has Hashable and % has finiteAggregate

InnerEvalable(S, S) if S has Evalable S and S has SetCategory

OrderedSet if S has OrderedSet and % has finiteAggregate

PartialOrder if S has OrderedSet and % has finiteAggregate

SetCategory if S has SetCategory