Contains operations for working with values of type Map.
Function or value | Description | ||
|
![]() ![]() ![]() ![]() ![]() ![]() 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.
Example
let input = Map [ (1, "a"); (2, "b") ] input |> Map.add 3 "c" // evaluates to map [(1, "a"); (2, "b"); (3, "c")] input |> Map.add 2 "aa" // evaluates to map [(1, "a"); (2, "aa")] |
||
|
Example
let input = Map [ (1, "a"); (2, "b") ] input |> Map.change 1 (fun x -> match x with | Some s -> Some (s + "z") | None -> None ) evaluates to map [(1, "az"); (2, "b")] |
||
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.containsKey 1 // evaluates to true sample |> Map.containsKey 3 // evaluates to false |
||
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.count // evaluates to 2 |
||
Example
let emptyMap = Map.empty<int, string> |
|||
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.exists (fun n s -> n = s.Length) // evaluates to true sample |> Map.exists (fun n s -> n < s.Length) // evaluates to false |
||
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.filter (fun n s -> n = s.Length) // evaluates to map [(1, "a")] |
||
Full Usage:
Map.find key table
Parameters:
'Key
-
The input key.
table : Map<'Key, 'T>
-
The input map.
Returns: 'T
The value mapped to the given key.
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.find 1 // evaluates to "a" sample |> Map.find 3 // throws KeyNotFoundException |
||
|
![]() ![]() ![]() ![]() ![]() ![]()
Evaluates the function on each mapping in the collection. Returns the key for the first mapping
where the function returns 'true'. Raise
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.findKey (fun n s -> n = s.Length) // evaluates to 1 sample |> Map.findKey (fun n s -> n < s.Length) // throws KeyNotFoundException |
||
Full Usage:
Map.fold folder state table
Parameters:
'State -> 'Key -> 'T -> 'State
-
The function to update the state given the input key/value pairs.
state : 'State
-
The initial state.
table : Map<'Key, 'T>
-
The input map.
Returns: 'State
The final state value.
|
Example
let sample = Map [ (1, "a"); (2, "b") ] ("initial", sample) ||> Map.fold (fun state n s -> sprintf "%s %i %s" state n s)Evaluates to "initial 1 a 2 b" .
|
||
Full Usage:
Map.foldBack folder table state
Parameters:
'Key -> 'T -> 'State -> 'State
-
The function to update the state given the input key/value pairs.
table : Map<'Key, 'T>
-
The input map.
state : 'State
-
The initial state.
Returns: 'State
The final state value.
|
Example
let sample = Map [ (1, "a"); (2, "b") ] (sample, "initial") ||> Map.foldBack (fun n s state -> sprintf "%i %s %s" n s state)Evaluates to "1 a 2 b initial"
|
||
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.forall (fun n s -> n >= s.Length) // evaluates to true sample |> Map.forall (fun n s -> n = s.Length) // evaluates to false |
||
|
Example
let emptyMap = Map.empty<int, string> emptyMap |> Map.isEmpty // evaluates to true let notEmptyMap = Map [ (1, "a"); (2, "b") ] emptyMap |> Map.isEmpty // evaluates to false |
||
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.iter (fun n s -> printf "%i %s " n s)Prints "1 a 2 b " .
|
||
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.keys // evaluates to seq [1; 2] |
||
|
![]() ![]() ![]() ![]() ![]() ![]() Builds a new collection whose elements are the results of applying the given function to each of the elements of the collection. The key passed to the function indicates the key of element being transformed.
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.map (fun n s -> sprintf "%i %s" n s) // evaluates to map [(1, "1 a"); (2, "2 b")] |
||
Full Usage:
Map.maxKeyValue table
Parameters:
Map<'Key, 'T>
-
The input map.
Returns: 'Key * 'T
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.maxKeyValue // evaluates to (2, "b") |
||
Full Usage:
Map.minKeyValue table
Parameters:
Map<'Key, 'T>
-
The input map.
Returns: 'Key * 'T
|
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.minKeyValue // evaluates to (1, "a") |
||
Full Usage:
Map.ofArray elements
Parameters:
('Key * 'T)[]
-
The input array of key/value pairs.
Returns: Map<'Key, 'T>
The resulting map.
|
Example
let input = [| (1, "a"); (2, "b") |] input |> Map.ofArray // evaluates to map [(1, "a"); (2, "b")] |
||
|
Example
let input = [ (1, "a"); (2, "b") ] input |> Map.ofList // evaluates to map [(1, "a"); (2, "b")] |
||
|
Example
let input = seq { (1, "a"); (2, "b") } input |> Map.ofSeq // evaluates to map [(1, "a"); (2, "b")] |
||
Full Usage:
Map.partition predicate table
Parameters:
'Key -> 'T -> bool
-
The function to test the input elements.
table : Map<'Key, 'T>
-
The input map.
Returns: Map<'Key, 'T> * Map<'Key, 'T>
A pair of maps in which the first contains the elements for which the predicate returned true
and the second containing the elements for which the predicated returned false.
|
![]() ![]() ![]() ![]() ![]() ![]() Builds two new maps, one containing the bindings for which the given predicate returns 'true', and the other the remaining bindings.
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.partition (fun n s -> n = s.Length) // evaluates to (map [(1, "a")], map [(2, "b")]) |
||
|
![]() ![]() ![]() ![]() ![]() ![]()
Searches the map looking for the first element where the given function returns a
Example
let sample = Map [ (1, "a"); (2, "b"); (10, "ccc"); (20, "ddd") ] sample |> Map.pick (fun n s -> if n > 5 && s.Length > 2 then Some s else None)Evaluates to "ccc"
Example
let sample = Map [ (1, "a"); (2, "b"); (10, "ccc"); (20, "ddd") ] sample |> Map.pick (fun n s -> if n > 5 && s.Length > 4 then Some s else None)Raises KeyNotFoundException
|
||
|
![]() ![]() ![]() ![]() ![]() ![]() Removes an element from the domain of the map. No exception is raised if the element is not present.
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.remove 1 // evaluates to map [(2, "b")] sample |> Map.remove 3 // equal to sample |
||
Full Usage:
Map.toArray table
Parameters:
Map<'Key, 'T>
-
The input map.
Returns: ('Key * 'T)[]
The array of key/value pairs.
|
![]() ![]() ![]() ![]() ![]() ![]() Returns an array of all key-value pairs in the mapping. The array will be ordered by the keys of the map.
Example
let input = Map [ (1, "a"); (2, "b") ] input |> Map.toArray // evaluates to [|(1, "a"); (2, "b")|] |
||
|
![]() ![]() ![]() ![]() ![]() ![]() Returns a list of all key-value pairs in the mapping. The list will be ordered by the keys of the map.
Example
let input = Map [ (1, "a"); (2, "b") ] input |> Map.toList // evaluates to [(1, "a"); (2, "b")] |
||
|
![]() ![]() ![]() ![]() ![]() ![]() Views the collection as an enumerable sequence of pairs. The sequence will be ordered by the keys of the map.
Example
let input = Map [ (1, "a"); (2, "b") ] input |> Map.toSeq // evaluates to seq [(1, "a"); (2, "b")] |
||
|
![]() ![]() ![]() ![]() ![]() ![]()
Lookup an element in the map, returning a
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.tryFind 1 // evaluates to Some "a" sample |> Map.tryFind 3 // evaluates to None |
||
Full Usage:
Map.tryFindKey predicate table
Parameters:
'Key -> 'T -> bool
-
The function to test the input elements.
table : Map<'Key, 'T>
-
The input map.
Returns: 'Key option
The first key for which the predicate returns true or None if the predicate evaluates to false for each key/value pair.
|
![]() ![]() ![]() ![]() ![]() ![]() Returns the key of the first mapping in the collection that satisfies the given predicate. Returns 'None' if no such element exists.
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.tryFindKey (fun n s -> n = s.Length) // evaluates to Some 1 sample |> Map.tryFindKey (fun n s -> n < s.Length) // evaluates to None |
||
|
Example
let sample = Map [ (1, "a"); (2, "b"); (10, "ccc"); (20, "ddd") ] sample |> Map.tryPick (fun n s -> if n > 5 && s.Length > 2 then Some s else None)Evaluates to Some "ccc" .
Example
let sample = Map [ (1, "a"); (2, "b"); (10, "ccc"); (20, "ddd") ] sample |> Map.tryPick (fun n s -> if n > 5 && s.Length > 4 then Some s else None)Evaluates to None .
|
||
|
![]() ![]() ![]() ![]() ![]() ![]() The values in the map, including the duplicates. The sequence will be ordered by the keys of the map.
Example
let sample = Map [ (1, "a"); (2, "b") ] sample |> Map.values // evaluates to seq ["a"; "b"] |