## `select` -- select operands

#### Introduction

`select(`object, f`)` returns a copy of the object with all operands removed that do not satisfy a criterion defined by the procedure `f`.

#### Call(s)

`select(object, f <, p1, p2...>)`

#### Parameters

`object` | - | a list, a set, a table, an expression sequence, or an expression of type `DOM_EXPR`
`f` | - | a procedure returning a Boolean value
`p1, p2...` | - | any MuPAD objects accepted by `f` as additional parameters

#### Returns

an object of the same type as the input object.

### Overloadable:

`object`

#### Related Functions

`map`, `op`, `split`, `zip`

#### Details

- `select` is a fast and handy function for picking out elements of lists, sets, tables etc. that satisfy a criterion set by the procedure `f`.
- The function `f` must return a value that can be evaluated to one of the Boolean values `TRUE`, `FALSE`, or `UNKNOWN`. It may either return one of these values directly, or it may return an equation or an inequality that can be simplified to one of these values by the function `bool`.
- Internally, the function `f` is applied to all operands `x` of the input object via the call `f(x, p1, p2, ...)`. If the result is not `TRUE`, this operand is removed. The original object is not modified in this process.

  The output object is of the same type as the input object, i.e., a list yields a list, a set yields a set etc.

- An input object that is an expression sequence is not flattened. Cf. example 2.
- Also ``atomic'' objects such as numbers or identifiers can be passed to `select` as first argument. Such objects are handled like sequences with a single operand.
- `select` is a function of the system kernel.

#### Example 1

`select` handles lists and sets. In the first example, we select all true statements from a list of logical statements. The result is again a list:

    >> select([1 = 1, 1 = 2, 2 = 1, 2 = 2], bool)

                                  [1 = 1, 2 = 2]

In the following example, we extract the subset of all elements that are recognized as zero by `iszero`:

    >> select({0, 1, x, 0.0, 4*x}, iszero)

                                     {0, 0.0}

`select` also works on tables:

    >> T:= table(1 = "y", 2 = "n", 3 = "n", 4 = "y", 5 = "y"):
       select(T, has, "y")

                                    table(
                                      5 = "y",
                                      4 = "y",
                                      1 = "y"
                                    )

The following expression is a sum, i.e., an expression of type `"_plus"`. We extract the sum of all terms that do not contain `x`:

    >> select(x^5 + 2*x + y - 4, _not@has, x)

                                       y - 4

We extract all factors containing `x` from the following product. The result is a product with exactly one factor, and therefore, is not of the syntactical type `"_mult"`:

    >> select(11*x^2*y*(1 - y), has, x)

                                         2
                                        x

    >> delete T:

#### Example 2

`select` works for expression sequences:

    >> select((1, -4, 3, 0, -5, -2), testtype, Type::Negative)

                                    -4, -5, -2

The $ command generates such expression sequences:

    >> select(i $ i = 1..20, isprime)

                            2, 3, 5, 7, 11, 13, 17, 19

Atomic objects are treated as expression sequences of length one:

    >> select(5, isprime)

                                         5

The following result is the void object `null()` ob type `DOM_NULL`:

    >> domtype(select(6, isprime))

                                     DOM_NULL

#### Example 3

It is possible to pass an ``anonymous procedure'' to `select`. This allows to perform more complex actions with one call. In the following example, the command `anames(All)` returns a set of all identifiers that have a value in the current MuPAD session. The `select` statement extracts all identifiers beginning with the letter `"h"`:

    >> select(anames(All), x -> expr2text(x)[0] = "h")

                  {has, hold, help, hastype, history, heaviside}

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000