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

Re: XQuery/XPath 3.1: Node List to Node Set ("distinct

Subject: Re: XQuery/XPath 3.1: Node List to Node Set ("distinct nodes")
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 29 Dec 2021 16:58:51 -0000
Re:  XQuery/XPath 3.1: Node List to Node Set ("distinct
On Wed, Dec 29, 2021 at 2:37 AM Michael Kay mike@xxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Here is an improved (and shorter) version of the fold-left() solution:
>
> fold-left($nodes, (), function($a, $n) { $a, $n except $a })
>

Let's compare the two:
$nodes => fold-left((), function($all, $this) {if ($all intersect $this)
then $all else ($all, $this)})
$nodes => fold-left((), function($all, $this) {$all, $this except $all})


Shorter, yes.

As for performance, doing the same measurement as earlier gives us 0.46ms
-- that is almost exactly the same as before (0.44ms) with a very small
additional time taken (0.02ms) which can safely be ignored.

Also, at least to me, subtracting a set from a singleton set seems
counter-intuitive, but I understand why this is done.

Cheers,
Dimitre


>
> Michael Kay
> Saxonica
>
> On 28 Dec 2021, at 23:47, Michael Kay mike@xxxxxxxxxxxx <
> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> 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
>
> On 28 Dec 2021, at 21:56, Michael Kay mike@xxxxxxxxxxxx <
> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> You might consider
>
> $nodes | ()
>
> a bit more intuitive.
>
> Michael Kay
> Saxonica
>
> On 28 Dec 2021, at 19:23, Eliot Kimber eliot.kimber@xxxxxxxxxxxxxx <
> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hmph.
>
> That is certainly much more efficient p
 but is not necessarily obvious
> (at least not to me).
>
> Thanks!
>
> 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: *Martin Honnen martin.honnen@xxxxxx <
> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
> *Date: *Tuesday, December 28, 2021 at 1:15 PM
> *To: *xsl-list@xxxxxxxxxxxxxxxxxxxxxx <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> *Subject: *Re:  XQuery/XPath 3.1: Node List to Node Set ("distinct
> nodes")
>
> [External Email]
>
>
> On 28.12.2021 20:10, Eliot Kimber eliot.kimber@xxxxxxxxxxxxxx wrote:
> > I couldnbt find an answer in my google and markmail searching so I
> > thought Ibd ask here:
> >
> > Given an arbitrary list of nodes that may contain duplicates, what is
> > the most efficient way to reduce the node list to a set?
> >
> > The solution I came up with is a recursive function:
> >
> > (:
> >
> > Get the unique nodes from the supplied sequence
> >
> > @param nodes The sequence of nodes to evaluate
> >
> > @return A sequence of nodes such that each node in $nodes exists exactly
> > once.
> >
> > :)
> >
> > declare function dutils:distinctNodes($nodes as node()*) as node()* {
> >
> >    dutils:_getDistinctNodes($nodes, ())
> >
> > };
> >
> > declare function dutils:_getDistinctNodes($nodes as node()*, $resultList
> > as node()*) as node()* {
> >
> >    if (exists($nodes))
> >
> >    then
> >
> >    let $node := head($nodes)
> >
> >    return dutils:_getDistinctNodes(tail($nodes), ($resultList | $node))
> >
> >    else $resultList
> >
> > };
> >
> > Which works but I feel like Ibm missing some obvious way to do this
more
> > directly, but Ibm not seeing it.
> >
> > Am I missing a better solution?
>
> $nodes/.
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
> email)
>
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
> email)
>
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
> email)
>
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/782854> (by
> email <>)
>


--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

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.