This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.
Hi, I am using map:merge to construct a map from smaller maps and would like to preserve values when the keys agree. For example, when calling map:merge((map:entry(0, "red"), (map:entry(1, "green"), map:entry(1, "blue"))) I would like to get back something like map { 0: "red", 1: ("green", "blue") } The W3C specification is to drop "green" in favor of "blue". While the desired behavior can be accomplished using XQuery syntax, the solution is not elegant and is relatively slow. Would it be possible to add an option to map:merge that will exhibit lossless merging? Thanks, Ron
Your desired function can be written function f:merge-entries($m1, $m2) { map:merge( for $k in distinct-values((map:keys($m1), map:keys($m2)) return map{$k : ($m1($k), $m2($k))} ) } which doesn't feel inelegant to me, and if it's inefficient, that's an implementation problem.
I would agree that it'd be nice to have a map:merge that can do that. I.e., perhaps one that takes a second argument that is a higher order function which takes a sequence of map entries that contain the same key. However, writing such a function is trivial (Michael shows one approach, which can be made higher-order), which you can then use as a generic variant of map:merge. I doubt that, considering the spec is in CR stage, new functions, or variants of functions can be added.
> I doubt that, considering the spec is in CR stage, new functions, or > variants of functions can be added. Apologies, I see now that this was raised against XQ 3.2 Use Cases.
Ok. But it gets more complicated if the merge is done repeatedly in a loop over a large database (e.g., 200,000 clinical trials). Adding an option in Java should be straightforward and the most efficient solution IMHO. Otherwise, feel free to close this suggestion.
This can now be done in 3.1 using the syntax map:merge($maps, map{"duplicates":"combine"})