Algosim documentation: GroupBy

GroupBy

Groups the elements of a container according to their properties.

Syntax

Description

If X is any container and f a function defined on X, then group(X, f) returns a list of lists – one for each value of f in X –, each containing all elements of X with this particular image under f.

If tag is a valid identifier, then group(X, f, tag) will instead return a list of structures, again one for each value of f in X. Each structure has two members: tag containing the f-image, and members, the list of values in X with this image.

group(X, f, tag, list) will use list as the name for the second structure members. Here, as well, list must be a valid identifier.

Examples

M ≔ RandomIntMatrix(10, 0, 100)
⎛98   3  50   6   5  45  71  36  34  13⎞
⎜90  24  43  51   6  58  63  40  78  43⎟
⎜85  93  45  84  32  23  69  10  52  17⎟
⎜52  36  88  49  69  47  77  98  31  56⎟
⎜99  65  45  68  83  12  63  58  29  54⎟
⎜26  26  12  80  37  15  71  97  26  85⎟
⎜ 7  98  97  79  34  21  60  35  59  45⎟
⎜90  27  91  24  75  28   1  77  26  42⎟
⎜79  78  84  47  67  99  35  27  58  23⎟
⎝ 6  13  97  17  37  44  20  30  12  16⎠
GroupBy(M, odd)
(12, 26, 26, 80, 34, 98, 26, 54, 98, 88, 36, 56, 58, 12, 68, 44, 6, 58, 20, 16, 12, 30, 84, 24, 90, 60, 28, 78, 42, 26, 52, 24, 58, 36, 90, 84, 40, 32, 6, 50, 34, 52, 98, 6, 10, 78)
(79, 13, 91, 27, 51, 43, 77, 1, 75, 97, 13, 45, 17, 3, 5, 37, 99, 67, 47, 35, 23, 71, 27, 45, 23, 45, 65, 83, 45, 29, 63, 99, 69, 49, 17, 47, 69, 31, 77, 97, 63, 7, 79, 59, 35, 21, 85, 43, 85, 93, 37, 97, 71, 15)
GroupBy(M, odd, "odd")
(odd: false, members: (12, 26, 26, 80, 34, 98, 26, 54, 98, 88, 36, 56, 58, 12, 68, 44, 6, 58, 20, 16, 12, 30, 84, 24, 90, 60, 28, 78, 42, 26, 52, 24, 58, 36, 90, 84, 40, 32, 6, 50, 34, 52, 98, 6, 10, 78))
(odd: true, members: (79, 13, 91, 27, 51, 43, 77, 1, 75, 97, 13, 45, 17, 3, 5, 37, 99, 67, 47, 35, 23, 71, 27, 45, 23, 45, 65, 83, 45, 29, 63, 99, 69, 49, 17, 47, 69, 31, 77, 97, 63, 7, 79, 59, 35, 21, 85, 43, 85, 93, 37, 97, 71, 15))
GroupBy(M, odd, "odd", "numbers")
(odd: false, numbers: (12, 26, 26, 80, 34, 98, 26, 54, 98, 88, 36, 56, 58, 12, 68, 44, 6, 58, 20, 16, 12, 30, 84, 24, 90, 60, 28, 78, 42, 26, 52, 24, 58, 36, 90, 84, 40, 32, 6, 50, 34, 52, 98, 6, 10, 78))
(odd: true, numbers: (79, 13, 91, 27, 51, 43, 77, 1, 75, 97, 13, 45, 17, 3, 5, 37, 99, 67, 47, 35, 23, 71, 27, 45, 23, 45, 65, 83, 45, 29, 63, 99, 69, 49, 17, 47, 69, 31, 77, 97, 63, 7, 79, 59, 35, 21, 85, 43, 85, 93, 37, 97, 71, 15))
GroupBy(M, n ↦ last(digits(n)), "LastDigit", "entries")
(LastDigit: 0, entries: (80, 10, 40, 90, 20, 60, 90, 50, 30))
(LastDigit: 1, entries: (51, 21, 91, 1, 71, 31, 71))
(LastDigit: 2, entries: (12, 12, 32, 52, 12, 42, 52))
(LastDigit: 3, entries: (83, 63, 23, 43, 13, 13, 23, 63, 93, 43, 3))
(LastDigit: 4, entries: (44, 24, 34, 84, 54, 24, 84, 34))
(LastDigit: 5, entries: (45, 35, 15, 85, 45, 5, 45, 35, 75, 85, 45, 65))
(LastDigit: 6, entries: (26, 6, 56, 6, 36, 26, 36, 26, 16, 26, 6))
(LastDigit: 7, entries: (27, 47, 77, 77, 27, 67, 17, 97, 7, 97, 37, 47, 97, 17, 37))
(LastDigit: 8, entries: (58, 98, 68, 98, 78, 58, 98, 28, 78, 88, 58))
(LastDigit: 9, entries: (99, 79, 49, 69, 79, 99, 59, 29, 69))
person ≔ (fn, ln, role) ↦ struct("FirstName": fn, "LastName": ln, "role": role);
L ≔ '(person("Albus", "Dumbledore", "headmaster"), person("Minerva", "McGonagall", "teacher"), person("Severus", "Snape", "teacher"), person("Pomona", "Sprout", "teacher"), person("Filius", "Flitwick", "teacher"), person("Horace", "Slughorn", "teacher"), person("Harry", "Potter", "student"), person("Ronald", "Weasley", "student"), person("Hermione", "Granger", "student"), person("Remus", "Lupin", "teacher"), person("Rubeus", "Hagrid", "teacher"), person("Luna", "Lovegood", "student"))
(FirstName: Albus, LastName: Dumbledore, role: headmaster)
(FirstName: Minerva, LastName: McGonagall, role: teacher)
(FirstName: Severus, LastName: Snape, role: teacher)
(FirstName: Pomona, LastName: Sprout, role: teacher)
(FirstName: Filius, LastName: Flitwick, role: teacher)
(FirstName: Horace, LastName: Slughorn, role: teacher)
(FirstName: Harry, LastName: Potter, role: student)
(FirstName: Ronald, LastName: Weasley, role: student)
(FirstName: Hermione, LastName: Granger, role: student)
(FirstName: Remus, LastName: Lupin, role: teacher)
(FirstName: Rubeus, LastName: Hagrid, role: teacher)
(FirstName: Luna, LastName: Lovegood, role: student)
ApplyIf(GroupBy(L, member("role"), "role"), IsStructure, member("LastName"), 3)
(role: headmaster, members: (Dumbledore))
(role: student, members: (Weasley, Granger, Potter, Lovegood))
(role: teacher, members: (Lupin, Hagrid, McGonagall, Snape, Sprout, Flitwick, Slughorn))

See also