![]() |
|
Navigation |
Synopsis Group elements in a set given an equivalence function.
Function
set[set[&T]] group(set[&T] input, bool (&T a, &T b) similar)
Usage
import Set;
Examples We classify animals by their number of legs.
rascal>import Set;
ok
Create a map from animals to number of legs.
rascal>legs = ("bird": 2, "dog": 4, "human": 2, "snake": 0, "spider": 8, "millepede": 1000, "crab": 8, "cat": 4);
map[str, int]: ("snake":0,"spider":8,"human":2,"crab":8,"cat":4,"bird":2,"dog":4,"millepede":1000)
Define function nLegs that returns the number of legs fro each animal (or 0 when the animal is unknown):
rascal>int nLegs(str animal){ >>>>>>> return legs[animal] ? 0; >>>>>>>} int (str): int nLegs(str); rascal>bool similar(str a, str b) = nLegs(a) == nLegs(b); bool (str, str): bool similar(str, str);Now group a set of animals: rascal>group({"bird", "dog", "human", "spider", "millepede", "zebra", "crab", "cat"}, similar);
set[set[str]]: {
{"spider"},
{"zebra"},
{"human"},
{"crab"},
{"cat"},
{"bird"},
{"dog"},
{"millepede"}
}
Questions
Question [1].
![]() ![]() ![]() |