select specified leading elements of a list, set, or rtable
select specified trailing elements of a list, set, or rtable
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)
(optional) integer; the number of elements to be selected
(optional) Boolean-valued selection procedure
list, set, or rtable; the object from which to select
b1, ..., bn
(optional) extra arguments passed to p
(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.
L ≔ seq⁡1..10
When applied to rtables, the result is converted to a list.
V ≔ Vector⁡1,2,3
When operating on sets the result may be platform dependent.
S ≔ abc,uvwxyz,1,∫sin⁡xⅆx
If p(e, b1, ..., bn) does not evaluate to a boolean an error is returned.
Error, (in ListTools:-SelectFirst) cannot determine if this expression is true or false: isprime(c)
Download Help Document