Map Functions
as_pairs
Converts a Map into an Array of Pairs. Since Maps are ordered, the output array will always have elements in the same order they were added to the Map.
Signatures
Array[Pair[P, Y]] as_pairs(Map[P, Y])Parameters
Map[P, Y]:Mapto convert toPairs.
Returns
- Ordered
ArrayofPairs, where each pair contains the key (left) and value (right) of aMapelement.
Example
Map[String, Int] x = {"a": 1, "c": 3, "b": 2}
Array[Pair[String, Int]] result = as_pairs(x)
# `result` now contains [("a", 1), ("c", 3), ("b", 2)].as_map
Converts an Array of Pairs into a Map in which the left elements of the Pairs are the keys and the right elements the values. All the keys must be unique, or an error is raised. The order of the key/value pairs in the output Map is the same as the order of the Pairs in the Array.
Signatures
Map[P, Y] as_map(Array[Pair[P, Y]])Parameters
Array[Pair[P, Y]]: Array ofPairs to convert to aMap.
Returns
Map[P, Y]of the elements in the input array.
Example
Array[Pair[String, Int]] x = [("a", 1), ("c", 3), ("b", 2)]
Map[String, Int] result = as_map(x)
# `result` now contains {"a": 1, "c": 3, "b": 2}.keys
Given a key-value type collection (Map, Struct, or Object), returns an Array of the keys from the input collection, in the same order as the elements in the collection.
When the argument is a Struct, the returned array will contain the keys in the same order they appear in the struct definition. When the argument is an Object, the returned array has no guaranteed order.
When the input Map or Object is empty, an empty array is returned.
Signatures
Array[P] keys(Map[P, Y])
Array[String] keys(Struct|Object)Parameters
Map[P, Y]|Struct|Object: Collection from which to extract keys.
Returns
Array[P]of the input collection's keys. If the input is aStructorObject, then the returned array will be of typeArray[String].
Example
Map[String, Int] x = {"a": 1, "b": 2, "c": 3}
Array[String] result = keys(x)
# `result` now contains ["a", "b", "c"].contains_key Requires WDL v1.2
Given a key-value type collection (Map, Struct, or Object) and a key, tests whether the collection contains an entry with the given key.
This function has three variants:
Boolean contains_key(Map[P, Y], P): Tests whether theMaphas an entry with the given key. IfPis an optional type (e.g.,String?), then the second argument may beNone.Boolean contains_key(Object, String): Tests whether theObjecthas an entry with the given name.Boolean contains_key(Map[String, Y]|Struct|Object, Array[String]): Tests recursively for the presence of a compound key within a nested collection.
For the third variant, the first argument is a collection that may be nested to any level, i.e., contain values that are collections, which themselves may contain collections, and so on. The second argument is an array of keys that are resolved recursively. If the value associated with any except the last key in the array is None or not a collection type, this function returns false.
For example, if the first argument is a Map[String, Map[String, Int]] and the second argument is ["foo", "bar"], then the outer Map is tested for the presence of key "foo", and if it is present, then its value is tested for the presence of key "bar". This only tests for the presence of the named element, not whether or not it is defined.
Signatures
Boolean contains_key(Map[P, Y], P)
Boolean contains_key(Object, String)
Boolean contains_key(Map[String, Y]|Struct|Object, Array[String])Parameters
Map[P, Y]|Struct|Object: Collection to search for the key.P|Array[String]: The key to search. If the first argument is aMap, then the key must be of the same type as theMap's key type. If theMap's key type is optional then the key may also be optional. If the first argument is aMap[String, Y],Struct, orObject, then the key may be either aStringorArray[String].
Returns
trueif the collection contains the key, otherwisefalse.
Example
Map[String, Int] m = {"a": 1, "b": 2}
Boolean has_a = contains_key(m, "a")
# `has_a` now contains `true`.
Boolean has_c = contains_key(m, "c")
# `has_c` now contains `false`.values Requires WDL v1.2
Returns an Array of the values from the input Map, in the same order as the elements in the map. If the map is empty, an empty array is returned.
Signatures
Array[Y] values(Map[P, Y])Parameters
Map[P, Y]:Mapfrom which to extract values.
Returns
Array[Y]of the inputMap's values.
Example
Map[String, Int] x = {"a": 1, "b": 2, "c": 3}
Array[Int] result = values(x)
# `result` now contains [1, 2, 3].collect_by_key
Given an Array of Pairs, creates a Map in which the right elements of the Pairs are grouped by the left elements. In other words, the input Array may have multiple Pairs with the same key. Rather than causing an error (as would happen with as_map), all the values with the same key are grouped together into an Array.
The order of the keys in the output Map is the same as the order of their first occurrence in the input Array. The order of the elements in the Map values is the same as their order of occurrence in the input Array.
Signatures
Map[P, Array[Y]] collect_by_key(Array[Pair[P, Y]])Parameters
Array[Pair[P, Y]]:ArrayofPairs to group.
Returns
Mapof keys toArrays of values.
Example
Array[Pair[String, Int]] x = [("a", 1), ("b", 2), ("a", 3)]
Map[String, Array[Int]] result = collect_by_key(x)
# `result` now contains {"a": [1, 3], "b": [2]}.