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.
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:
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.
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:
|Add||In this function, a new map is created by adding the binding to the map that is currently given.|
|ContainsKey||The element is checked to see if it is part of the map’s domain.|
|Count||Counts how many bindings are contained in the map.|
|IsEmpty||This method returns true if the map does not contain any bindings.|
|Item||Find an element in the map by looking it up. When there is no binding in the map, it throws a KeyNotFoundException.|
|Remove||The element is removed from the map’s domain. In the absence of the element, no exception is raised.|
|TryFind||If 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:
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)]
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.