Changes

Jump to navigation Jump to search
350 bytes added ,  12:59, 23 April 2011
no edit summary
The function call <code>[[#get|map:get($map, $key)]]</code> can be used to retrieve the value associated with a given key.
A <em>map</em> can also be viewed as a function from keys to associated values. To achieve this, a map is also a function item. The function corresponding to the map has the signature <code>function($key as xs:anyAtomicType) as item()*</code>. Calling the function has the same effect as calling the <code>get</code> function: the expression <code>$map($key)</code> returns the same result as <code>map:get($map, $key)</code>. For example, if <code>$books-by-isbn</code> is a map whose keys are ISBNs and whose assocated values are <code>book</code> elements, then the expression <code>$books-by-isbn("0470192747")</code> returns the <code>book</code> element with the given ISBN. The fact that a map is a function item allows it to be passed as an argument to higher-order functions that expect a function item as one of their arguments. As an example, the following query uses the higher-order function <code>fn:map($f, $seq)</code> to extract all bound values from a <em>map</em>: <pre class="brush:xquery">let $map := map { 'foo' := 42, 'bar' := 'baz', 123 := 456 }return fn:map($map, map:keys($map))</pre> This returns some permutation of <code>(42, 'baz', 456)</code>.
Like all other values, <em>maps</em> are immutable. For example, the <code>map:remove</code> function creates a new map by removing an entry from an existing map, but the existing map is not changed by the operation.
Like sequences, <em>maps</em> have no identity. It is meaningful to compare the contents of two maps, but there is no way of asking whether they are "the same map": two maps with the same content are indistinguishable.
Because a map is a function item, functions that apply to functions also apply to maps. A map is an anonymous function, so <code>fn:function-name</code> returns the empty sequence; <code>fn:function-arity</code> always returns <code>1</code>.
Maps may be compared using the <code>fn:deep-equal</code> function. The semantics for this function are extended so that when two items are compared, at any level of recursion, the items compare equal if they are both maps, if both use the same collation, if both contain the same set of keys (compared using the <code>eq</code> operator), without regard to ordering, and if for each key that is present in both maps, the associated values are deep-equal. When comparing maps, the maps' collation is used rather than the collation supplied as an argument to the <code>fn:deep-equal</code> function.
editor, reviewer
33

edits

Navigation menu