Hi Joseph,
On 29.01.2017 at 22:55, meumapple wrote (with possible deletions):
As for the fold-left, is it a way to express what could also be written as a recursive function?
yes it is! `fn:fold-left($seq, $acc, $f)` can be implemented as the following recursive function:
declare function local:fold-left($seq, $acc, $f) { if(empty($seq)) then $acc else local:fold-left( tail($seq), $f($acc, head($seq)), $f ) };
The whole function call can thus be rewritten to the following query with specialized function:
declare function local:count-elems($seq, $acc) { if(empty($seq)) then $acc else local:count-elems( tail($seq), let $key := $seq[1]/string(), $nums := $acc[1], $n := ($nums($key), 0)[1] + 1 return ( map:merge(($nums, map{ $key: $n })), tail($acc), <a n="{$n}">{$key}</a> ) ) }; let $src:= <c> <a>red</a> <a>red</a> <a>blue</a> <a>green</a> <a>green</a> </c> return tail( local:count-elems( $src/*, map{} ) )
Cheers, Leo