[xquery-talk] Manipulating Maps In Non-Trivial Ways

Eliot Kimber ekimber at contrext.com
Sun Jul 19 14:55:28 PDT 2015


Yes, the fold operations are still a bit too clever for my taste--I prefer
to make my business a logic a little more verbose to clearer, at least to
me--I don't want to have to think too deeply when I'm programming :-)

Cheers,

E.
----
Eliot Kimber, Owner
Contrext, LLC
http://contrext.com




On 7/19/15, 12:38 PM, "Michael Kay" <mike at saxonica.com> wrote:

>A fold can sometimes be useful for this kind of operation:
>
>$newKeys => fold-left($m1, function($key, $map) { $map => map:put($key,
>($map($key), $obj) })
>
>Also, appending an item to the value for a key is a sufficiently common
>operation it¹s worth having a function for it:
>
>declare  function f:map-add($map, $key, $value) as map(*) {
>   map:put($map, $key, ($map($key), $value))
>}
>
>and then the fold becomes
>
>$newKeys => fold-left($m1, function($key, $map) {$map => f:add($key,
>$obj)})
>
>But it takes a while before folds become intuitive. map:merge works
>equally well.
>
>Michael Kay
>Saxonica
>
>> On 19 Jul 2015, at 16:40, Eliot Kimber <ekimber at contrext.com> wrote:
>> 
>> I don't know why I'm having such a hard time figuring out how to operate
>> on maps but I just can't get my head around it (I suspect procedural
>>brain
>> damage but it could just be advancing age). I suspect others will or are
>> having the same challenge.
>> 
>> My challenge of the day is:
>> 
>> I have an existing map where each entry is a sequence mapped to a string
>> key, e.g.:
>> 
>> let $m1 := map{ 'key1' : (), 'key2' : () }
>> 
>> I then have a node and a set of key values. For each key I need to find
>> the entry with that key and add the node to the entry, e.g.
>> 
>> let $newKeys := ('key1', 'key3')
>> let $obj := <added/>
>> 
>> Let $m2 := for $key in $newKeys
>>               (: Find sequence for key,
>>                  Add obj to sequence,
>>                  Update map entry for new key
>>                :)
>> 
>> I think the right answer is to use map:merge like so:
>> 
>> let $m2 := 
>>  map:merge(
>>    $m1,
>>    lor $key in $newKeys
>>        Map {$key, ($m1($key), $obj))}
>>  )
>> 
>> Is my analysis correct? Is there a better way to do this sort of map
>> updating?
>> 
>> Thanks,
>> 
>> Eliot
>> ----
>> Eliot Kimber, Owner
>> Contrext, LLC
>> http://contrext.com
>> 
>> 
>> 
>> _______________________________________________
>> talk at x-query.com
>> http://x-query.com/mailman/listinfo/talk
>
>





More information about the talk mailing list