ListTools
|
select specified leading elements of a list, set, or rtable
|
|
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
|
|
| (1) |
>
|
|
>
|
|
>
|
|
>
|
|
>
|
|
When applied to rtables, the result is converted to a list.
>
|
|
>
|
|
>
|
|
>
|
|
When operating on sets the result may be platform dependent.
>
|
|
| (14) |
| (15) |
If p(e, b1, ..., bn) does not evaluate to a boolean an error is returned.
>
|
|
|
|
|
|
|
|