Maps are similar to sets, but have additional constraints. The following example illustrates:
void System::main([string] args):
amap = {"Hello"->1,"World"->2}
out.println(amap)
amap["Hello"] = 2
out.println(str(amap))
Here, “amap” is a map from string keys to int values, where every key maps to exactly one value (but not necessarily vice-versa). Thus, the second println statement will print {"Hello"->2,"World"->2}.
There are some interesting operations supported for maps in Whiley:
void System::main([string] args):
map = {"Hello"->1,"World"->2}
map["Hello","World"] = 3
rmap = ~map // take inverse of map
out.println(str(rmap))
Here, we see a batch assignment of keys where both keys "Hello" and "World" are updated to hold value 3. Also, ~map computes the inverse of map which, in this case, gives: {3->{"Hello","World"}}.

Popular Posts