Articles

  • No categories

Sets

Whiley supports resizeable sets, as well as maps. Like lists, these automatically resize themselves to ensure there is enough space for all their elements. The following illustrates the main operations for sets:

void ::main(System.Console console):
    aset = {1,2,3,4}
    bset = {2,3,4}
    if !(aset contains bset):
        aset = aset + bset
    if 1 in bset:
        out.println("GOT 1")
    else:
        aset = aset & {4,5}
    console.out.println("SET SIZE: " + str(|aset|))

Here we can see the usual set operations: union (+), intersection (&) and element-of (in). The length of a set is obtained with |...|, as for lists. Also, whilst not shown, set difference operator is given by ‘-‘ as expected.