xsl-list
[Top] [All Lists]

Re: [xsl] XQuery/XPath 3.1: Node List to Node Set ("distinct nodes")

2021-12-29 12:33:39

 
So given this expression:
 
fold-left($nodes, (), function($a, $n) { $a, $n except $a })
 
I understand this to be iterating over $nodes from left to right, applying 
the function function($a, $n) to each node, where $a is the next node and $n 
is the accumulated value (being the result returned by the function on each 
invocation).
 
The “$a, $n except $a” in the function body constructs a new sequence of ($a, 
$n), excluding $a if it is already in $n. This sequence is then passed as the 
second parameter of the next invocation of the function. This has the effect 
of preserving the order of input node list.
 

Almost but not quite. $a is the set of distinct nodes found so far, $n is the 
node currently being examined. So ($n except $a) is the node currently being 
examined if it isn't present in $a, and is the empty sequence otherwise.

As Dimitre says, ($n except $a) where $n is a singleton isn't immediately 
intuitive - but intuition takes practice!

As it happens, Saxon probably does a better job of optimizing the more complex 
expression $n[not(. intersect $a)] -- though it's still O(n*m).

Michael Kay
Saxonica
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>