Articles

  • No categories

Maps

Maps are similar to sets, but have additional constraints. The following example illustrates:

void ::main(System.Console console):
    amap = {"Hello"=>1,"World"=>2}
    console.out.println(amap)
    amap["Hello"] = 2
    console.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 ::main(System.Console console):
    map = {"Hello"=>1,"World"=>2}
    map["Hello","World"] = 3
    rmap = ~map // take inverse of map
    console.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"}}.