F# Maps

An F# map is an object class that stores a collection of keys and values that are associated with a key.

A map can be thought of as a way of storing and retrieving data based on a unique identifier, something similar to dictionaries in other programming languages.

F# by default makes maps immutable, meaning that once a map has been created, its contents can never be modified once it has been created.

The operation on a map returns a new map that contains the changes that were made instead of returning an existing map. Because of this, maps are ideal as a part of functional programming, which is a discipline that places a high value on immutability.

We will be discussing the basics of maps in F# in this article, including creating, adding, removing, and querying elements based on the map.



Creating Maps

In order to create a map, we first create an empty map with Map.empty and then add items to the map using the Add function.

Using the following example, we will demonstrate how this can be achieved:

Example: 

let actors = Map.empty. //Creating an empty Map Add("Jennifer Aniston", "1969"). Add("Kevin Hart", "1979"). Add("Matt LeBlanc", "1967"). Add("Morgan Freeman", "1937"). Add("Samuel L. Jackson", "1948");; printfn "Map – Actors: %A" actors(* Convert a list to Map *) let capitals = [ "Canada", "Ottawa"; "United States", "Washington, D.C."; "Chili", "Santiago"; "Barbados", "Bridgetown"; "Denmark", "Nuuk" ] |> Map.ofList;; printfn "Map capitals of USA : %A" capitals

And the output will be:

Map - Actors: map [(Jennifer Aniston, 1969); (Kevin Hart, 1979); (Matt LeBlanc, 1967); (Morgan Freeman, 1937); (Samuel L. Jackson, 1948)]
Map capitals of USA : map [(Barbados, Bridgetown); (Canada, Ottawa); (Chili, Santiago); (Denmark, Nuuk); (United States, Washington, D.C.)]

By using the key, you will be able to access individual elements within the map.

Example: 

let actors = Map.empty. //Creating an empty Map Add("Jennifer Aniston", "1969"). Add("Kevin Hart", "1979"). Add("Matt LeBlanc", "1967"). Add("Morgan Freeman", "1937"). Add("Samuel L. Jackson", "1948");; printfn "Map – Actors: %A" actors//Accessing an elements using key printfn "\n%A" actors.["Morgan Freeman"] printfn "%A" actors.["Kevin Hart"]
The following example results for accessing individual elements are as follows:
Map - Actors: map [(Jennifer Aniston, 1969); (Kevin Hart, 1979); (Matt LeBlanc, 1967); (Morgan Freeman, 1937); (Samuel L. Jackson, 1948)]

1937
1979

Basic Operations on Maps

Add module name

Below is a table showing the basic operations you can perform on a map:

MemberOverview
AddIn this function, a new map is created by adding the binding to the map that is currently given.
ContainsKeyThe element is checked to see if it is part of the map’s domain.
CountCounts how many bindings are contained in the map.
IsEmptyThis method returns true if the map does not contain any bindings.
ItemFind an element in the map by looking it up. When there is no binding in the map, it throws a KeyNotFoundException.
RemoveThe element is removed from the map’s domain. In the absence of the element, no exception is raised.
TryFindIf the element is in the map’s domain, this method returns a Some value. Otherwise, it returns a None value.

Some of the above functionalities are demonstrated in the following example:

Example: 

let actors = Map.empty. //Creating an empty Map Add("Jennifer Aniston", "1969"). Add("Kevin Hart", "1979"). Add("Matt LeBlanc", "1967"). Add("Morgan Freeman", "1937"). Add("Samuel L. Jackson", "1948");; printfn "Map – Actors: %A" actors(* finding the DOB of a Actors*) let foundDob = actors.TryFind "Samuel L. Jackson" match foundDob with | Some x -> printfn "\nFound: %s" x | None -> printfn "Did not find the specified value."let remove = actors |> Map.remove "Matt LeBlanc" printfn "\nUpdatedMap – Actors: %A" remove

The output of the following result will be as follows:

Map - Actors: map [(Jennifer Aniston, 1969); (Kevin Hart, 1979); (Matt LeBlanc, 1967); (Morgan Freeman, 1937); (Samuel L. Jackson, 1948)]

Found: 1948

UpdatedMap - Actors: map [(Jennifer Aniston, 1969); (Kevin Hart, 1979); (Morgan Freeman, 1937); (Samuel L. Jackson, 1948)]

Example Explanation

This F# code creates a map called actors with string keys representing actor names and string values representing their year of birth. The map is created by starting with an empty map using the Map.empty function, and then using the Add method to add elements to it one by one.

The printfn function prints the entire actors map to the console.

Next, the TryFind method is used to search for actor “Samuel L. Jackson’s birth year in the actors map. The method returns an option type, which can either be Some x if the key is found and its value is bound to x, or None if the key is not found. The match expression is used to pattern match the result of TryFind and print the corresponding message to the console.

Finally, the Map.remove function is used to remove the element with key “Matt LeBlanc” from the actors map, and the resulting map is stored in a new variable called remove. The printfn function is used again to print the entire remove map to the console.

Overall, this code demonstrates how to create F# map, add elements to it, search for elements using TryFind, remove elements using Map.remove, and print the entire map to the console.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *