假设我有这样的地图:
map[str,int] a = ("apple": 1, "pear": 2, "banana": 3, "kiwi": 4);
有没有办法返回地图中的所有键,即“苹果”,“梨”,“香蕉”,“猕猴桃”?根据{{3}}的文档,似乎没有内置的方式。有可能吗?
答案 0 :(得分:1)
多种方法:
首先只是投影第一列:
rascal>map[str,int] a = ("apple": 1, "pear": 2, "banana": 3, "kiwi": 4);
map[str, int]: ("banana":3,"pear":2,"kiwi":4,"apple":1)
rascal>a<0>
set[str]: {"banana","pear","kiwi","apple"}
第二个方法是标记列,并执行相同的操作(<>
表示法或.
表示法)
rascal>map[str fruit, int count] b = ("apple": 1, "pear": 2, "banana": 3, "kiwi": 4);
map[str fruit, int count]: ("banana":3,"pear":2,"kiwi":4,"apple":1)
rascal>b<fruit>
set[str]: {"banana","pear","kiwi","apple"}
rascal>b.fruit
set[str]: {"banana","pear","kiwi","apple"}
最后,地图也是它们自己的键的生成器,如:
rascal>[ f | f <- a]
list[str]: ["banana","pear","kiwi","apple"]
rascal>{ f | f <- a}
set[str]: {"banana","pear","kiwi","apple"}
rascal>import IO;
rascal>for (f <- a)
>>>>>>> println(f);
banana
pear
kiwi
apple