Algosim documentation: filter

filter

Filters the elements of a container using a predicate.

Syntax

Description

If X is a non-planar container and p a predicate, then filter(X, p) returns the object obtained from X by removing all elements for which p is false.

For instance, if X is the list '(1, 2, 3, 4), then filter(X, odd) returns the list '(1, 3). If X is the vector ❨1, 2, 3, 4❩, then filter(X, odd) returns the vector ❨1, 3❩. But if X is the matrix ❨❨1, 2❩, ❨3, 4❩❩, filter(X, odd) is an error, because there is no obvious general way to remove specific entries from a matrix. The pick function can be used to obtain – as a list – those matrix entries that satisfy p.

Examples

ℙ ≔ compute(prime(n), n, 1, 1000000);
filter(ℙ, p ↦ (d ≔ digits(p); ∑(d) = ∏(d)))
2
3
5
7
2141
2411
4211
11251
12511
15121
21221
25111
1112171
1127111
1172111
1271111
7112111
11112811
11128111
11218111
12111811
12118111
12181111

See also