SelectFirst - Maple Help

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 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

 > $\mathrm{with}\left(\mathrm{ListTools}\right):$
 > $L≔\left[\mathrm{seq}\left(1..10\right)\right]$
 ${L}{≔}\left[{1}{,}{2}{,}{3}{,}{4}{,}{5}{,}{6}{,}{7}{,}{8}{,}{9}{,}{10}\right]$ (1)
 > $\mathrm{SelectFirst}\left(L\right)$
 ${1}$ (2)
 > $\mathrm{SelectFirst}\left(1,L\right)$
 $\left[{1}\right]$ (3)
 > $\mathrm{SelectFirst}\left(\mathrm{isprime},L\right)$
 ${2}$ (4)
 > $\mathrm{SelectLast}\left(\mathrm{isprime},L\right)$
 ${7}$ (5)
 > $\mathrm{SelectFirst}\left(3,\mathrm{type},L,\mathrm{even}\right)$
 $\left[{2}{,}{4}{,}{6}\right]$ (6)
 > $\mathrm{SelectFirst}\left(-3,\mathrm{type},L,\mathrm{odd}\right)$
 $\left[{9}{,}{7}{,}{5}\right]$ (7)
 > $\mathrm{SelectLast}\left(3,\mathrm{type},L,\mathrm{odd}\right)$
 $\left[{5}{,}{7}{,}{9}\right]$ (8)

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

 > $V≔\mathrm{Vector}\left(\left[1,2,3\right]\right)$
 ${V}{≔}\left[\begin{array}{c}{1}\\ {2}\\ {3}\end{array}\right]$ (9)
 > $\mathrm{SelectFirst}\left(2,V\right)$
 $\left[{1}{,}{2}\right]$ (10)
 > $\mathrm{SelectLast}\left(3,⟨⟨1|2|3|4⟩,⟨5|6|7|8⟩⟩\right)$
 $\left[{7}{,}{4}{,}{8}\right]$ (11)
 > $\mathrm{SelectFirst}\left(2,\mathrm{type},\left[a,"a",a,"b"\right],\mathrm{name},\mathrm{output}=\mathrm{indices}\right)$
 $\left[{1}{,}{3}\right]$ (12)
 > $\mathrm{SelectFirst}\left(2,\mathrm{type},\left[a,"a",a,"b"\right],\mathrm{string},\mathrm{output}=\left[\mathrm{values},\mathrm{indices}\right]\right)$
 $\left[{"a"}{,}{"b"}\right]{,}\left[{2}{,}{4}\right]$ (13)

When operating on sets the result may be platform dependent.

 > $S≔\left\{1,"abc",\mathrm{uvwxyz},\mathrm{Int}\left(\mathrm{sin}\left(x\right),x\right)\right\}$
 ${S}{≔}\left\{{1}{,}{"abc"}{,}{\mathrm{uvwxyz}}{,}{\int }{\mathrm{sin}}{}\left({x}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}\right\}$ (14)
 > $\mathrm{SelectFirst}\left(3,S\right)$
 $\left\{{1}{,}{"abc"}{,}{\mathrm{uvwxyz}}\right\}$ (15)

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

 > $\mathrm{SelectLast}\left(3,\mathrm{isprime},\left[a,b,c\right]\right)$