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

scan -- apply a function to each element in a list or sequence

Synopsis

Description

scan(L, f) applies the function f to each element of the list L. The function values are discarded.

i1 : scan({a, 4, "George", 2^100}, print)
a
4
George
1267650600228229401496703205376
i2 : scan("foo", print)
f
o
o

scan(n, f) applies the function f to each element of the list 0, 1, ..., n-1

i3 : scan(4, print)
0
1
2
3
i4 : v = {a,b,c}; scan(#v, i -> print(i,v#i))
(0, a)
(1, b)
(2, c)

The keyword break can be used to terminate the scan prematurely, and optionally to specify a return value for the expression. Here we use it to locate the first even number in a list.

i6 : scan({3,5,7,11,44,55,77}, i -> if even i then break i)

o6 = 44

If L is an instance of a class with the iterator method installed (e.g., a string), then f is applied to the values obtained by repeatedly calling next on the output of iterator L until StopIteration is returned.

i7 : scan("foo", print)
f
o
o

See also

Ways to use scan :

For the programmer

The object scan is a compiled function.