Discussion:
[xquery-talk] map:union function?
Graydon Saunders
2015-07-18 17:12:04 UTC
Permalink
Hello --

map:merge returns only the last value associated with the key of a map.
Any other values are lost.

Sometimes something that returns all of the values is wanted; my particular
present use case is wanting to make sure I've got all the errors associated
with a particular XML node when each rule returns a map using the node
identifier as the key and the error code as the value, but I don't think
it's hard to think of other use cases.

It's certainly possible to write one's own function to do this but there's
several possible approaches. A built-in could be more readily optimized by
the XQuery processor. (Or at least I hope so!)

So where map:merge looks like:

map:merge($maps as map(*)*) as map(*)

so would

map:union($maps as map(*)*) as map(*)

The only change would be:

The supplied maps are combined as follows:

1.

There is one entry in the returned map for each distinct key present in
the union of the input maps, where two keys are distinct if they are not
the ·same key· <http://www.w3.org/TR/xpath-functions-31/#dt-same-key>.
2.

The associated value for each such key is *a sequence of values* taken
from *every *map in the input sequence $input that contains an entry
with this key.

Would this be a useful thing to add, generall? (It would certainly be
useful to me!)

Thanks!
Graydon
Michael Kay
2015-07-18 17:59:11 UTC
Permalink
I’ve certainly come across the requirement a few times. It’s not too hard to code:

map:merge(
for $k in distinct-values($maps!map:keys())
return map:entry($k, $maps?($k))
)

and we’re at a stage with 3.1 where there’s very little chance of getting a function added if it can be coded as a 2-liner.

Michael Kay
Saxonica
Post by Graydon Saunders
Hello --
map:merge returns only the last value associated with the key of a map. Any other values are lost.
Sometimes something that returns all of the values is wanted; my particular present use case is wanting to make sure I've got all the errors associated with a particular XML node when each rule returns a map using the node identifier as the key and the error code as the value, but I don't think it's hard to think of other use cases.
It's certainly possible to write one's own function to do this but there's several possible approaches. A built-in could be more readily optimized by the XQuery processor. (Or at least I hope so!)
map:merge($maps as map(*)*) as map(*)
so would
map:union($maps as map(*)*) as map(*)
There is one entry in the returned map for each distinct key present in the union of the input maps, where two keys are distinct if they are not the ·same key· <http://www.w3.org/TR/xpath-functions-31/#dt-same-key>.
The associated value for each such key is a sequence of values taken from every map in the input sequence $input that contains an entry with this key.
Would this be a useful thing to add, generall? (It would certainly be useful to me!)
Thanks!
Graydon
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Loading...