13.07.2015 Views

5 Model Programs

5 Model Programs

5 Model Programs

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Systems with Complex State 163system. For example, a server may have an operation that assigns integer prioritiesto its active clients:static Map priority = Map.EmptyMap;[Action]static void SetPriority(string client, int p){priority = priority.Override(client, p);}The Override method produces a new map by substituting a key/value pair. If thekey given as the argument is not in the map, then a new key/value pair is added. Inthis example we set the variable priority to contain the value of the new map.Creating mapsIf your map contains five or fewer key/value pairs, you can use a map constructorthat takes keys and values as arguments:Map cityId = new Map("Athens", 1, "Rome", 2,"Paris", 3, "New York", 4);Assert.AreEqual(cityId["Paris"], 3);A common way to build a map is to start with the empty map and add elementsprogrammatically:Map idCity = Map.EmptyMap;string[] cities =new string[]{"Athens", "Rome", "Paris", "New York"};for(int i = 0; i < cities.Length; i += 1)idCity = idCity.Add(i + 100, city[i]);Assert.AreEqual(idCity[102], "Paris");Map lookup operationsThe lookup operations for a map are very similar to those of a .NET dictionary.If you need to look up the value of a key, you may use the C# indexer syntax:Map cityState =new Map("Athens", "GA", "Paris", "TX"};Assert.AreEqual(cityState["Paris"], "TX");

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!