Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » lists and sequences » MutableList
next | previous | forward | backward | up | index | toc

MutableList -- the class of all mutable lists

Description

For an overview of lists and sequences, see lists and sequences.

Normally the entries in a mutable list are not printed, to prevent infinite loops in the printing routines. To print them out, use peek.

i1 : s = new MutableList from {a,b,c};
i2 : s#2 = 1234

o2 = 1234
i3 : s

o3 = MutableList{...3...}

o3 : MutableList
i4 : peek s

o4 = MutableList{a, b, 1234}

To construct a mutable list with a given length and whose entries are all null, provide an integer.

i5 : s = new MutableList from 6;
i6 : peek s

o6 = MutableList{null, null, null, null, null, null}

If a value is assigned at a position that is not yet present in a mutable list, then it grows accordingly, and any intermediate positions are filled with null.

i7 : t = new MutableList from {};
i8 : t#4 = e

o8 = e

o8 : Symbol
i9 : peek t

o9 = MutableList{null, null, null, null, e}

When working with mutable lists, be careful not to turn a linear algorithm into a quadratic one by repeatedly appending to it. Compare the following:

i10 : s = new MutableList

o10 = MutableList{}

o10 : MutableList
i11 : elapsedTime scan(1000, i -> s#i = i^2) -- quadratic, since we grow s at each step
 -- .0042196s elapsed
i12 : t = new MutableList from 1000

o12 = MutableList{...1000...}

o12 : MutableList
i13 : elapsedTime scan(1000, i -> t#i = i^2) -- linear
 -- .000477384s elapsed

See also

Menu

Types of mutable list:

  • Bag -- the class of all bags

Methods that use a mutable list:

  • new MutableList from ZZ
  • positions(MutableList,Function) -- see positions -- which elements of a list satisfy a condition
  • remove(MutableList,ZZ) -- see remove -- remove an entry from a mutable hash table, list, or database
  • shuffle(MutableList) -- see shuffle -- shuffle a list randomly

For the programmer

The object MutableList is a type, with ancestor classes BasicList < Thing.


The source of this document is in /build/reproducible-path/macaulay2-1.26.05+ds/M2/Macaulay2/packages/Macaulay2Doc/ov_lists.m2:838:0.