F Sharp Programming/Sets and Maps

In addition to lists and sequences, F# provides two related immutable data structures called sets and maps. Unlike lists, sets and maps are unordered data structures, meaning that these collections do not preserve the order of elements as they are inserted, nor do they permit duplicates.

Sets
A set in F# is just a container for items. Sets do not preserve the order in which items are inserted, nor do they allow duplicate entries to be inserted into the collection.

Sets can be created in a variety of ways:

Adding an item to an empty set The  module contains a useful function   which returns an empty set to start with.

Conveniently, all instances of sets have an  function with the type. In other words, our  returns a new set containing our new item, which makes it easy to add items together in this fashion:

Converting lists and sequences into sets Additionally, the we can use  and   to convert an entire collection into a set:

The example above demonstrates the unordered nature of sets.

The Module
The FSharp.Collections.Set module contains a variety of useful methods for working with sets.

 
 * Return a new set with an element added to the set. No exception is raised if the set already contains the given element.

 
 * Compare two sets. Places sets into a total order.

 
 * Return the number of elements in the set. Same as "size".

 
 * Return a new set with the elements of the second set removed from the first. That is a set containing only those items from the first set that are not also in the second set.

 
 * Test if any element of the collection satisfies the given predicate.

 
 * Return a new collection containing only the elements of the collection for which the given predicate returns "true".

 
 * Compute the intersection, or overlap, of the two sets.

 
 * Return a new collection containing the results of applying the given function to each element of the input set.

 
 * Evaluates to  if the given element is in the given set.

 
 * Return a new set with the given element removed. No exception is raised if the set doesn't contain the given element.

 
 * Return the number of elements in the set.

 
 * Evaluates to "true" if all elements of the first set are in the second.

 
 * Evaluates to "true" if all elements of the first set are in the second, and there is at least one element in the second set which is not in the first.

 
 * Compute the union of the two sets.

Examples
shakespeare: "O Romeo, Romeo! wherefore art thou Romeo?" shakespeareArray: 0: O 1: Romeo 2: Romeo 3: wherefore 4: art 5: thou 6: Romeo shakespeareSet: 0: O 1: Romeo 2: art 3: thou 4: wherefore

Maps
A map is a special kind of set: it associates keys with values. A map is created in a similar way to sets:

You can use the  to access elements in the map:

The Module
The FSharp.Collections.Map module handles map operations.

 
 * Return a new map with the binding added to the given map.

 
 * Returns an empty map.

 
 * Return true if the given predicate returns true for one of the bindings in the map.

 
 * Build a new map containing only the bindings for which the given predicate returns.

 
 * Lookup an element in the map, raising  if no binding exists in the map.

 
 * Test if an element is in the domain of the map.

 
 * Remove an element from the domain of the map. No exception is raised if the element is not present.

 
 * Lookup an element in the map, returning a  value if the element is in the domain of the map and   if not.

Examples
Find a capital by country (type 'q' to quit): Egypt The capital of Egypt is Cairo Find a capital by country (type 'q' to quit): Slovenia The capital of Slovenia is Ljubljana Find a capital by country (type 'q' to quit): Latveria Country not found. Find a capital by country (type 'q' to quit): USA The capital of USA is Washington D.C. Find a capital by country (type 'q' to quit): q Bye bye

Set and Map Performance
F# sets and maps are implemented as immutable AVL trees, an efficient data structure which forms a self-balancing binary tree. AVL trees are well-known for their efficiency, in which they can search, insert, and delete elements in the tree in O(log n) time, where n is the number of elements in the tree.