Macaulay2 » Documentation
Packages » Macaulay2Doc :: iterator
next | previous | forward | backward | up | index | toc

iterator -- get an iterator

Synopsis

Description

An iterator is an object that is used to traverse through x. Usually, but not necessarily, this will be an instance of the Iterator class.

i1 : iter = iterator {1, 2, 3}

o1 = iterator {1, 2, 3}

o1 : Iterator

The class of an iterator should have a next method installed that gets the next element of x.

i2 : next iter

o2 = 1
i3 : next iter

o3 = 2
i4 : next iter

o4 = 3

If x contains only a finite number of elements, then next should return the symbol StopIteration after exhausting them all.

i5 : next iter

o5 = StopIteration

o5 : Symbol

Instances of classes with this method installed can be used like lists in for loops and scan.

i6 : lookup(iterator, String)

o6 = FunctionClosure[../../../../../Macaulay2/m2/iterators.m2:19:26-26:12]

o6 : FunctionClosure
i7 : for i in "foo" list i

o7 = {f, o, o}

o7 : List
i8 : scan("foo", print)
f
o
o

They can also be passed to apply and select. In each case, an Iterator object will be returned that is an iterator for itself.

i9 : apply("foo", toUpper)

o9 = (F, O, O)

o9 : Sequence
i10 : for i in oo list i

o10 = {F, O, O}

o10 : List
i11 : select("foo", i -> i == "o")

o11 = iterator "foo"

o11 : Iterator
i12 : for i in oo list i

o12 = {o, o}

o12 : List

See also

Ways to use iterator :

For the programmer

The object iterator is a method function with a single argument.