ListTools
SelectFirst
select specified leading elements of a list, set, or rtable
SelectLast
select specified trailing elements of a list, set, or rtable
Calling Sequence
Parameters
Description
Examples
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)
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.
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.
with⁡ListTools:
L≔seq⁡1..10
L≔1,2,3,4,5,6,7,8,9,10
SelectFirst⁡L
1
SelectFirst⁡1,L
SelectFirst⁡isprime,L
2
SelectLast⁡isprime,L
7
SelectFirst⁡3,type,L,even
2,4,6
SelectFirst⁡−3,type,L,odd
9,7,5
SelectLast⁡3,type,L,odd
5,7,9
When applied to rtables, the result is converted to a list.
V≔Vector⁡1,2,3
V≔123
SelectFirst⁡2,V
1,2
SelectLast⁡3,1|2|3|4,5|6|7|8
7,4,8
SelectFirst⁡2,type,a,a,a,b,name,output=indices
1,3
SelectFirst⁡2,type,a,a,a,b,string,output=values,indices
a,b,2,4
When operating on sets the result may be platform dependent.
S≔1,abc,uvwxyz,Int⁡sin⁡x,x
S≔1,abc,uvwxyz,∫sin⁡xⅆx
SelectFirst⁡3,S
1,abc,uvwxyz
If p(e, b1, ..., bn) does not evaluate to a boolean an error is returned.
SelectLast⁡3,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
Matrix
rtable
select
Vector
Download Help Document