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

position -- the first element of a list satisfying a condition

Synopsis

Description

position(A, f) returns the smallest index i such that f(A#i) is true. If no element satisfies the condition, null is returned.

i1 : position((10,20,43,105,6,93), odd)

o1 = 2
i2 : position((10,20,43,105,6,93), i -> i<0)

Use position(A, B, f) to return the smallest index i such that f(A#i, B#i) is true.

i3 : position((10,20,43,105,6,93),(18,82,12,7,35,92), (a,b) -> a>b)

o3 = 2

The Reverse option will return the largest index instead.

i4 : position((10,20,43,105,6,93), odd, Reverse => true)

o4 = 5
i5 : position((10,20,43,105,6,93),(18,82,12,7,35,92), (a,b) -> a>b, Reverse => true)

o5 = 5

To find all indices of elements satisfying the condition, see positions. To return the elements, rather than their indices, see select. The function number counts the number of elements satisfying the condition.

i6 : positions((10,20,43,105,6,93), odd)

o6 = {2, 3, 5}

o6 : List
i7 : select((10,20,43,105,6,93), odd)

o7 = (43, 105, 93)

o7 : Sequence
i8 : number((10,20,43,105,6,93), odd)

o8 = 3

See also

Ways to use position :

For the programmer

The object position is a method function with options.