[xquery-talk] From map entry pairs to a pair of arrays

W.S. Hager wshager at gmail.com
Sat Jul 15 13:29:10 PDT 2017


Hi Joe,

In addition to other comments: also the size of the data matters, and not
to mention the purpose of what you're trying to achieve... Small chunks are
usually processed faster, so unless you just need to convert all data,
perhaps limiting the set could be useful. Especially when you need yet
another result, and the array would just be an intermediary result,
performance may vary across implementations.

Obviously this operation could be implemented in the host language to make
it truly efficient.

Cheers,
Wouter

Op 15 jul. 2017 21:12 schreef "Christian Grün" <christian.gruen at gmail.com>:

> Hi Joe, hi Emmanuel,
>
> Here is yet another alternative that can be efficient (but it’s not
> the most compact one):
>
>   let $map := map { "a": 1, "b": 2, "c": 3 }
>   return (
>     array { map:keys($map) },
>     array { map:for-each($map, function($key, $value) { $value }) }
>   )
>
> Last but not least, map:get($map, ...) can be replaced with $map(...):
>
>   let $map := map { "a": 1, "b": 2, "c": 3 }
>   let $keys := map:keys($map)
>   return (
>     array { $keys },
>     array { $keys ! $map(.) }
>   )
>
> Depending on the XQuery processor, and even the specific version, one
> or the other solution may be most efficient.
>
> Cheers,
> Christian
>
>
>
> On Sat, Jul 15, 2017 at 8:16 PM, Joe Wicentowski <joewiz at gmail.com> wrote:
> > Great, thank you, Emmanuel!  That is much better.
> >
> > I might just offer one enhancement - switching from a FLWOR to the simple
> > map operator:
> >
> > ```
> > xquery version "3.1";
> >
> > let $letters-numbers := map { "a": 1, "b": 2, "c": 3 }
> > let $keys := map:keys($letters-numbers)
> > return (
> >     array { $keys },
> >     array { $keys ! map:get($letters-numbers, .) }
> > )
> > ```
> >
> > Joe
> >
> > On Sat, Jul 15, 2017 at 12:12 PM Emmanuel Chateau <emchateau at laposte.net
> >
> > wrote:
> >>
> >> Hi,
> >> I would say
> >>
> >> ```
> >> let $letters-numbers := map { "a": 1, "b": 2, "c": 3 }
> >> let $keys := map:keys($letters-numbers)
> >> return (
> >>   array { $keys },
> >>   array { for $key in $keys return map:get($letters-numbers, $key) }
> >> )
> >> ```
> >> > Le 15 juil. 2017 à 11:41, Joe Wicentowski <joewiz at gmail.com> a écrit
> :
> >> >
> >> > Hi all,
> >> >
> >> > Is there an efficient way to transform entries in a map into two
> arrays,
> >> > one containing the keys and one containing the values?  Here's my
> attempt:
> >> >
> >> > ```
> >> > xquery version "3.1";
> >> >
> >> > let $letters-numbers := map { "a": 1, "b": 2, "c": 3 }
> >> > return
> >> >     (
> >> >         array { map:keys($letters-numbers) },
> >> >         array { $letters-numbers?* }
> >> >     )
> >> > ```
> >> >
> >> > This successfully takes `map { "a": 1, "b": 2, "c": 3 }` and returns
> >> > (["a", "b", "c"], [1, 2, 3]), but the way it iterates through the map
> twice
> >> > strikes me as somewhat inefficient.  Is there a better way to reduce
> this to
> >> > a single pass through the map, or is this actually the best approach?
> >> >
> >> > Joe
> >> > _______________________________________________
> >> > talk at x-query.com
> >> > http://x-query.com/mailman/listinfo/talk
> >>
> >
> > _______________________________________________
> > talk at x-query.com
> > http://x-query.com/mailman/listinfo/talk
>
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://x-query.com/pipermail/talk/attachments/20170715/548ebdb9/attachment.html>


More information about the talk mailing list