如何从 Rascal 地图中获取所有钥匙?
How to get all keys from a Rascal map?
假设我有这样一张地图:
map[str,int] a = ("apple": 1, "pear": 2, "banana": 3, "kiwi": 4);
有什么方法可以 return 地图中的所有键,即 "apple"、"pear"、"banana"、"kiwi"?从 http://tutor.rascal-mpl.org/Rascal/Expressions/Values/Map/Comprehension/Comprehension.html#/Rascal/Expressions/Values/Map/Map.html 的文档来看,似乎没有内置的方法。可能吗?
多种方式:
首先是投影出第一列:
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
假设我有这样一张地图:
map[str,int] a = ("apple": 1, "pear": 2, "banana": 3, "kiwi": 4);
有什么方法可以 return 地图中的所有键,即 "apple"、"pear"、"banana"、"kiwi"?从 http://tutor.rascal-mpl.org/Rascal/Expressions/Values/Map/Comprehension/Comprehension.html#/Rascal/Expressions/Values/Map/Map.html 的文档来看,似乎没有内置的方法。可能吗?
多种方式:
首先是投影出第一列:
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