Algosim documentation: pick

pick

Returns a list of all elements that satisfy a given predicate.

Syntax

Description

If X is any container and p a predicate, then pick(X, p) returns the list of all elements x of X for which p(x) is true.

pick(X, p, d) returns the list of all elements x of the containers at level d in X for which p(x) is true, level 1 corresponding to X itself, level 2 to the elements of X, and so on.

The order of the elements of pick(X, p[, d]) is undefined if the elements are partly or fully picked from sets.

If X is a list, then pick(X, p) = filter(X, p).

Examples

s ≔ ExampleData("Alice in Wonderland");
frequencies(pick(s, ChrIsPunctuation))
(’, 1756)
(., 990)
(-, 669)
(,, 2418)
(:, 233)
(‘, 1115)
(?, 202)
((, 51)
(), 56)
(;, 194)
(!, 450)
([, 7)
(“, 61)
(”, 52)
(*, 60)
(_, 4)
(], 2)
L ≔ compute((a − b) ⋅ (2⋅a + b^2), a, 1, 100, b, 1, 100);
pick(L, IsPrime)
failure
An object of type integer was expected as argument 1, but an object of type
array was given.
Call stack: IsPrime, pick
pick(L, IsPrime, 2)
5
17
37
101
197
257
401
577
677
1297
1601
2917
3137
4357
5477
7057
8101
8837
A ≔ RandomIntMatrix(100, 100, 0, 10000000) \ 5
⎛4119049   492790  3835296  6854822   677364     ⋯   ⎞
⎜3556172  1014920  5657822  4673304  6531465     ⋯   ⎟
⎜6490854  7073707  2439173  8819880  7477122     ⋯   ⎟
⎜1710869  2771588  3009408  2534200  2659355     ⋯   ⎟
⎜4828215   589944  9382372  1420148  5270921     ⋯   ⎟
⎝   ⋮        ⋮        ⋮        ⋮        ⋮        ⋱   ⎠
pick(A, n ↦ string(n) = reverse(string(n)))
7073707
7006007
36463
62326
9880889
1612161
298892
9104019
6919196
510015
8112118
4036304
L ≔ '(1, 2, "dog", '(5, 2), '('(10, 52, 22, "cat", RandomMatrix(2)), π))
1
2
dog
(5, 2)
((10, 52, 22, cat, ((0.170356541174, 0.336530831177), (0.371417297749, 0.266364166513))), 3.14159265359)
pick(L, IsNumber)
1
2
pick(L, IsNumber, 2)
5
2
3.14159265359
pick(L, IsNumber, 3)
10
52
22
pick(L, IsNumber, 4)
0.170356541174
0.336530831177
0.371417297749
0.266364166513
pick(L, IsNumber, 5)


See also