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

Subject: Re: [xsl] 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:50:57 -0000
On Wed, Dec 29, 2021 at 7:25 AM Eliot Kimber eliot.kimber@xxxxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> 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 b$a, $n except $ab 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.

Actually, $a is the accumulated result, and $n is the head of the remaining
sequence -- as per spec.

And if it were as quated above, we would get the in the results the last
occurrence of each item, not the first.

Cheers,
Dimitre


On Wed, Dec 29, 2021 at 7:25 AM Eliot Kimber eliot.kimber@xxxxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Wowbthis generated much more discussion than I anticipated.
>
>
>
> For my requirements of the moment the order of the result node set is
> unimportant, so b$nodes intersect ()b seems like the least surprising
> solution (and I suspect I might have arrived at it if Ibd spent a bit
more
> time thinking about the problem, but itbs the holidays and I had things
to
> do and places to be b&).
>
>
>
> Ibve never actually taken the time to understand how fold-left works, so
> this is an opportunity to learn.
>
>
>
> From the functions and operators spec:
>
>
>
> <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 b$a, $n except $ab 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 itbs 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: [xsl] 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 <
> 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
>
>
>
> 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