select specified leading elements of a list, set, or rtable - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Data Types : Tables, lists, and sets : ListTools Package : ListTools/SelectFirst

ListTools[SelectFirst] - select specified leading elements of a list, set, or rtable

ListTools[SelectLast] - select specified trailing elements of a list, set, or rtable

Calling Sequence

SelectFirst(p, L, b1, ..., bn, opts)

SelectFirst(n, p, L, b1, ..., bn, opts)

SelectLast(p, L, b1, ..., bn, opts)

SelectLast(n, p, L, b1, ..., bn, opts)

Parameters

n

-

(optional) integer; the number of elements to be selected

p

-

(optional) Boolean-valued selection procedure

L

-

list, set, or rtable; the object from which to select

b1, ..., bn

-

(optional) extra arguments passed to p

opts

-

(optional) equation of the form output = form, where form is one of values, indices, or a list containing values and/or indices.

Description

• 

The SelectFirst(n, p, L, b1, ..., bn) function selects the first n elements e of L which satisfy p(e, b1, ..., bn).

• 

The SelectLast(n, p, L, b1, ..., bn) function selects the last n elements e of L which satisfy p(e, b1, ..., bn).

• 

If n is not given, the default value of n is 1.

• 

If n is negative, SelectFirst searches backward from the end of L, and SelectLast searches forward from the start of L but reverses the output.

• 

If p is not given, the default value of p is the function which returns true.

• 

If L is an rtable (such as an Array, Matrix, or Vector), the result is equivalent to applying this function to convert(L,list) (with all other arguments the same).  (See convert/list.)

• 

If n is not given, the SelectFirst and SelectLast commands return the found element or NULL. Otherwise, if L is a list or rtable, a list is returned, and if L is a set, a set is returned.

• 

If the option output = form is given, then the elements selected and/or their indices in L are returned, depending on whether form includes the keywords values and/or indices, respectively. If the option output=form is not given, the default value of output is values. The return format is an expression sequence of the lists of values and/or indices (or the value and/or index, or NULL, if n is not given), the order reflecting the order of the keywords in output. The value of output may not include indices when L is a set. Any argument which is interpreted as the output = form option will not be passed as an extra argument to p.

Examples

withListTools:

L:=seqn,n=1..10

L:=1,2,3,4,5,6,7,8,9,10

(1)

SelectFirstL

1

(2)

SelectFirst1,L

1

(3)

SelectFirstisprime,L

2

(4)

SelectLastisprime,L

7

(5)

SelectFirst3,type,L,even

2,4,6

(6)

SelectFirst3,type,L,odd

9,7,5

(7)

SelectLast3,type,L,odd

5,7,9

(8)

When applied to rtables, the result is converted to a list.

V:=Vector1,2,3

V:=123

(9)

SelectFirst2,V

1,2

(10)

SelectLast3,1|2|3|4,5|6|7|8

7,4,8

(11)

SelectFirst2,type,a,a,a,b,name,output=indices

1,3

(12)

SelectFirst2,type,a,a,a,b,string,output=values,indices

a,b,2,4

(13)

When operating on sets the result may be platform dependent.

S:=abc,uvwxyz,1,∫sinxⅆx

S:=1,abc,uvwxyz,∫sinxⅆx

(14)

SelectFirst3,S

1,abc,uvwxyz

(15)

If p(e, b1, ..., bn) does not evaluate to a boolean an error is returned.

SelectLast3,isprime,a,b,c

Error, (in ListTools:-SelectFirst) cannot determine if this expression is true or false: isprime(c)

See Also

Array, convert/list, list, ListTools, Matrix, rtable, select, Vector


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam