Previous Page Next Page Contents

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

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




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000