[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

[no subject]

[no subject]
<lq>
Fold-left

Processes the supplied sequence from left to right, applying the supplied
function repeatedly to each item in turn, together with an accumulated result
value.
</lq>

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.

If I have this right then its essentially equivalent to my original recursive
solution but much, much simpler and of course usable directly as an XPath
expression.

Cheers,

E.


_____________________________________________
Eliot Kimber
Sr Staff Content Engineer
O: 512 554 9368
M: 512 554 9368
servicenow.com<https://www.servicenow.com>
LinkedIn<https://www.linkedin.com/company/servicenow> |
Twitter<https://twitter.com/servicenow> |
YouTube<https://www.youtube.com/user/servicenowinc> |
Facebook<https://www.facebook.com/servicenow>

From: Michael Kay mike@xxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wednesday, December 29, 2021 at 4:37 AM
To: xsl-list <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re:  XQuery/XPath 3.1: Node List to Node Set ("distinct nodes")
[External Email]

Here is an improved (and shorter) version of the fold-left() solution:

fold-left($nodes, (), function($a, $n) { $a, $n except $a })

Michael Kay
Saxonica


On 28 Dec 2021, at 23:47, Michael Kay
mike@xxxxxxxxxxxx<mailto:mike@xxxxxxxxxxxx>
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx<mailto:xsl-list-service@xxxxxxxxxxxx
rytech.com>> wrote:

For a solution that delivers distinct nodes in order of first appearance, my
preference would be

$nodes => fold-left((), function($all, $this) {if ($all intersect $this) then
$all else ($all, $this)})

It's likely to be O(n^2) in most implementations, whereas Martin Honnen's
solution is probably O(n log n) -- but this one is XPath rather than XQuery,
and feels more elegant.

Michael Kay
Saxonica

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.