![]() |
|
Navigation |
Synopsis Classify elements in a set.
Function
map[&K,set[&V]] classify(set[&V] input, &K (&V) getClass)
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 for each animal (or 0 when the animal is unknown):
rascal>int nLegs(str animal){ >>>>>>> return legs[animal] ? 0; >>>>>>>} int (str): int nLegs(str);Now classify a set of animals: rascal>classify({"bird", "dog", "human", "spider", "millepede", "zebra", "crab", "cat"}, nLegs);
map[int, set[str]]: (
0:{"zebra"},
2:{"human","bird"},
4:{"cat","dog"},
8:{"spider","crab"},
1000:{"millepede"}
)
Questions
Question [1].
![]() ![]() ![]() |