F# – Maps

  • Post author:
  • Post category:F#
  • Post comments:1 Comment
F# - Maps

F# maps is a special kind of set that associates the values with keys. A map is created in similar way assets are created.

Creating F# Maps

Maps are created by creating an empty map using Map. empty and adding items using the Add function. The following example demonstrates this −

Example

(* Create an empty Map *)
let students =
   Map.empty. (* Creating an empty Map *)
      Add("Zara Ali", "1501").
      Add("Rishita Gupta", "1502").
      Add("Robin Sahoo", "1503").
      Add("Gillian Megan", "1504");;
printfn "Map - students: %A" students

(* Convert a list to Map *)
let capitals =
   [ "Argentina", "Buenos Aires";
      "France ", "Paris";
      "Chili", "Santiago";
      "Malaysia", " Kuala Lumpur";
      "Switzerland", "Bern" ]
   |> Map.ofList;;
printfn "Map capitals : %A" capitals

When you compile and execute the program, it yields the following output −

Map - students: map
[("Gillian Megan", "1504"); ("Rishita Gupta", "1502"); ("Robin Sahoo", "1503
");
("Zara Ali", "1501")]
Map capitals : map
[("Argentina", "Buenos Aires"); ("Chili", "Santiago"); ("France ", "Paris");
("Malaysia", " Kuala Lumpur"); ("Switzerland", "Bern")]

You can access individual elements in the map using the key.

Example

(* Create an empty Map *)
let students =
   Map.empty. (* Creating an empty Map *)
      Add("Zara Ali", "1501").
      Add("Rishita Gupta", "1502").
      Add("Robin Sahoo", "1503").
      Add("Gillian Megan", "1504");;
printfn "Map - students: %A" students

(*Accessing an element using key *)
printfn "%A" students.["Zara Ali"]

When you compile and execute the program, it yields the following output −

Map - students: map
[("Gillian Megan", "1504"); ("Rishita Gupta", "1502"); ("Robin Sahoo", "1503
");
("Zara Ali", "1501")]
"1501"

Basic Operations on Maps

Add module name

The following table shows the basic operations on maps −

MemberDescription
AddReturns a new map with the binding added to the given map.
ContainsKeyTests if an element is in the domain of the map.
CountThe number of bindings on the map.
IsEmptyReturns true if there are no bindings on the map.
ItemLookup an element in the map. Raises KeyNotFoundException if no binding exists in the map.
RemoveRemoves an element from the domain of the map. No exception is raised if the element is not present.
tryingLookup an element in the map, returning a Some value if the element is in the domain of the map and None if not.

The following example demonstrates the uses of some of the above functionalities −

Example

(* Create an empty Map *)
let students =
   Map.empty. (* Creating an empty Map *)
      Add("Zara Ali", "1501").
      Add("Rishita Gupta", "1502").
      Add("Robin Sahoo", "1503").
      Add("Gillian Megan", "1504").
      Add("Shraddha Dubey", "1505").
      Add("Novonil Sarker", "1506").
      Add("Joan Paul", "1507");;
printfn "Map - students: %A" students
printfn "Map - number of students: %d" students.Count

(* finding the registration number of a student*)
let found = students.TryFind "Rishita Gupta"
match found with
| Some x -> printfn "Found %s." x
| None -> printfn "Did not find the specified value."

When you compile and execute the program, it yields the following output −

Map - students: map
[("Gillian Megan", "1504"); ("Joan Paul", "1507"); ("Novonil Sarker", "1506"
);
("Rishita Gupta", "1502"); ("Robin Sahoo", "1503");
("Shraddha Dubey", "1505"); ("Zara Ali", "1501")]
Map - number of students: 7
Found 1502.

Next Topic – Click Here

This Post Has One Comment

Leave a Reply