Or, retweeked and made for multiple maps (@ Marc thanx for the inspiration)

 

declare function XQR.map:merge

      ( $mapSet        as map(*)*

      , $mergeFunction as function(*)

      )                as map(*)

      {

        fold-left( $mapSet

                 , map {}

                 , function( $targetMap

                           , $sourceMap

                           )

                           {

                             map:merge(( $targetMap

                                       , $sourceMap

                                       , for-each( map:keys( $targetMap)[ . = map:keys( $sourceMap)]

                                                 , function( $key

                                                           )

                                                           {

                                                             map:entry( $key

                                                                      , $mergeFunction( map:get( $targetMap, $key)

                                                                                      , map:get( $sourceMap, $key)

                                                                                      )

                                                                      )

                                                           }

                                                 )

                                      ))

                           }

                 )

      } ;

 

Use:

 

xqr.map:merge( ( map { 'a' : ( 1,2,3,4)

                     , 'b' : ( 4,5,6,7)

                     }

               , map { 'b' : ( 6,7,8,9)

                     , '7' : ( 'x', 'y', 'z')

                     }

               , map { 'b' : ( 9,10,12)

                     , '7' : ( 'x', 'y', 'p')

                     }

               )

             , function( $data1

                       , $data2 

                       )

                       {

                         distinct-values(( $data1, $data2))

                       }

             ) ! map:serialize( .)

 

You’re welcome,

Rob

 

Van: basex-talk-bounces@mailman.uni-konstanz.de [mailto:basex-talk-bounces@mailman.uni-konstanz.de] Namens Graydon Saunders
Verzonden: woensdag 15 juli 2015 20:01
Aan: BaseX
Onderwerp: [basex-talk] Union of maps?

 

Hi --

 

In BaseX 8.2.1, I have a bunch of maps; the maps may have some overlap of key values.  I would like to merge/find the union of all of these maps while retaining all the values associated with each key in the resulting map-of-all-maps.  (Combined map?  Any word can be wrong, here! :)

 

map:merge() doesn't do this;  http://docs.basex.org/wiki/Map_Module#map:merge says

"The associated value for each such key is taken from the last map in the input sequence $input that contains an entry with this key." and testing confirms this.

 

Is there a straightforward way to do this that I'm missing?

 

Thanks!

Graydon




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com