FSharp.Core


Map<'Key, 'Value> Type

Immutable maps based on binary trees, where keys are ordered by F# generic comparison. By default comparison is the F# structural comparison function or uses implementations of the IComparable interface on key values.

See the Map module for further operations on maps. All members of this class are thread-safe and may be used concurrently from multiple threads.

Constructors

Constructor Description

Map(elements)

Full Usage: Map(elements)

Parameters:
    elements : seq<'Key * 'Value> - The input sequence of key/value pairs.

Returns: Map<'Key, 'Value> The resulting map.

Builds a map that contains the bindings of the given IEnumerable.

elements : seq<'Key * 'Value>

The input sequence of key/value pairs.

Returns: Map<'Key, 'Value>

The resulting map.

Example

 Map [ (1, "a"); (2, "b") ] // evaluates to map [(1, "a"); (2, "b")]

Instance members

Instance member Description

this.Add

Full Usage: this.Add

Parameters:
    key : 'Key - The key to add.
    value : 'Value - The value to add.

Returns: Map<'Key, 'Value> The resulting map.

Returns a new map with the binding added to the given map. If a binding with the given key already exists in the input map, the existing binding is replaced by the new binding in the result map.

key : 'Key

The key to add.

value : 'Value

The value to add.

Returns: Map<'Key, 'Value>

The resulting map.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Add (3, "c") // evaluates to map [(1, "a"); (2, "b"); (3, "c")]
 sample.Add (2, "aa") // evaluates to map [(1, "a"); (2, "aa")]

this.Change

Full Usage: this.Change

Parameters:
    key : 'Key - The input key.
    f : 'Value option -> 'Value option - The change function.

Returns: Map<'Key, 'Value> The resulting map.

Returns a new map with the value stored under key changed according to f.

key : 'Key

The input key.

f : 'Value option -> 'Value option

The change function.

Returns: Map<'Key, 'Value>

The resulting map.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 let f x =
     match x with
     | Some s -> Some (s + "z")
     | None -> None

 sample.Change (1, f) // evaluates to map [(1, "az"); (2, "b")]

this.ContainsKey

Full Usage: this.ContainsKey

Parameters:
    key : 'Key - The input key.

Returns: bool True if the map contains the given key.

Tests if an element is in the domain of the map.

key : 'Key

The input key.

Returns: bool

True if the map contains the given key.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.ContainsKey 1 // evaluates to true
 sample.ContainsKey 3 // evaluates to false

this.Count

Full Usage: this.Count

Returns: int

The number of bindings in the map.

Returns: int
Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Count // evaluates to 2

this.IsEmpty

Full Usage: this.IsEmpty

Returns: bool

Returns true if there are no bindings in the map.

Returns: bool
Example

 let emptyMap: Map<int, string> = Map.empty
 emptyMap.IsEmpty // evaluates to true

 let notEmptyMap = Map [ (1, "a"); (2, "b") ]
 notEmptyMap.IsEmpty // evaluates to false

this.[key]

Full Usage: this.[key]

Parameters:
    key : 'Key - The input key.

Returns: 'Value The value mapped to the key.

Lookup an element in the map. Raise KeyNotFoundException if no binding exists in the map.

key : 'Key

The input key.

Returns: 'Value

The value mapped to the key.

KeyNotFoundException Thrown when the key is not found.
Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.[1] // evaluates to "a"
 sample.[3] // throws KeyNotFoundException

this.Keys

Full Usage: this.Keys

Returns: ICollection<'Key>

The keys in the map. The sequence will be ordered by the keys of the map.

Returns: ICollection<'Key>
Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Keys // evaluates to seq [1; 2]

this.Remove

Full Usage: this.Remove

Parameters:
    key : 'Key - The input key.

Returns: Map<'Key, 'Value> The resulting map.

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

key : 'Key

The input key.

Returns: Map<'Key, 'Value>

The resulting map.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Remove 1 // evaluates to map [(2, "b")]
 sample.Remove 3 // equal to sample

this.TryFind

Full Usage: this.TryFind

Parameters:
    key : 'Key - The input key.

Returns: 'Value option The mapped value, or None if the key is not in the map.

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

key : 'Key

The input key.

Returns: 'Value option

The mapped value, or None if the key is not in the map.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.TryFind 1 // evaluates to Some "a"
 sample.TryFind 3 // evaluates to None

this.TryGetValue

Full Usage: this.TryGetValue

Parameters:
    key : 'Key - The input key.
    value : byref<'Value> - A reference to the output value.

Returns: bool true if the value is present, false if not.

Lookup an element in the map, assigning to value if the element is in the domain of the map and returning false if not.

key : 'Key

The input key.

value : byref<'Value>

A reference to the output value.

Returns: bool

true if the value is present, false if not.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.TryGetValue 1 // evaluates to (true, "a")
 sample.TryGetValue 3 // evaluates to (false, null)

 let mutable x = ""
 sample.TryGetValue (1, &x) // evaluates to true, x set to "a"

 let mutable y = ""
 sample.TryGetValue (3, &y) // evaluates to false, y unchanged

this.Values

Full Usage: this.Values

Returns: ICollection<'Value>

All the values in the map, including the duplicates. The sequence will be ordered by the keys of the map.

Returns: ICollection<'Value>
Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Values // evaluates to seq ["a"; "b"]