Re: fn:foot, fn:truncate, fn:item-at

The inspiration for slice() came from JSONPath, which itself appears to have got it from Python.

We could devise custom syntax for the operation, for example $input[start ; end (; step)] but a function seems more powerful and just as usable.

The main problem with relying on predicates for positional subscripting is the fact that the context changes in the predicate, which means that when you write things like

(1 to $N) ! (first-names[.] || last-names[.])

it doesn't do at all what you intended.

Similarly $input[$N] doesn't do what you intended if $N is a node that needs to be atomized (classic pitfall in XSLT when you do <xsl:variable><xsl:value-of select="$N+1"/></xsl:variable> - though we won't train them out of that if we haven't already).

In addition doing commonly-needed things like $input[position() mod 2 = 0] is just really clumsy.

subsequence() doesn't have the problems of the context changing, but writing subsequence($input, $N, 1) to get the item at position N is terribly long-winded compared with the equivalent in other languages, and the fact that it allows non-integer values also complicates the implementation no end, as well as making errors harder to debug.

Typical example today:

<xsl:for-each select="first-names">
  <xsl:variable name="p" select="position()"/>
  <xsl:value-of select=". || last-names[$p]"/>
</xsl:for-each>

With slice:
first-names ! (. || last-names => slice(position()))

(OK, there's another solution using for-each-pair - but the more you use higher-order-functions, the more useful it is to deliver functionality using functions rather than custom syntax.)

Michael Kay
Saxonica

> On 2 Dec 2020, at 15:27, Dimitre Novatchev <dnovatchev@gmail.com> wrote:
> 
> > slice($seq, -1)  to get the last item
> 
> > slice($seq, $N) to get the Nth item
> 
> > slice($seq, 1 to count($seq)-1) to get all but the last
> 
> -10  !
> 
> $seq[last()]
> 
> $seq[$n]
> 
> $seq[position() lt last()]
> 
> All of these are shorter than the proposed expressions containing slice() ! Why bother learn a new name and why pollute the function-name space?
> 
> This is just an example where we are going counter - Exupery and with close to no justification.
> 
> We also have another good friend: 
>   subsequence()
> 
> 
> Thanks,
> Dimitre
> 
> 
> On Wed, Dec 2, 2020 at 3:16 AM Michael Kay <mike@saxonica.com <mailto:mike@saxonica.com>> wrote:
> The latest version allows
> 
> slice($seq, -1)  to get the last item
> 
> slice($seq, $N) to get the Nth item
> 
> slice($seq, 1 to count($seq)-1) to get all but the last
> 
> and I thought that was probably good enough to make the individual functions unnecessary.
> 
> Would you agree?
> 
> Michael Kay
> Saxonica
> 
>> On 2 Dec 2020, at 10:30, Christian Grün <cg@basex.org <mailto:cg@basex.org>> wrote:
>> 
>> Hi Michael,
>> 
>> In your first public XQFO proposal [1], I have found seen three
>> functions (fn:foot, fn:truncate, fn:item-at) that have not made it
>> into the latest specification documents. Are you still working on it,
>> or did you deliberately decide not to include them in the new draft?
>> 
>> Thanks,
>> Christian
>> 
>> [1] https://www.saxonica.com/qt4specs/FO/Overview-diff.html <https://www.saxonica.com/qt4specs/FO/Overview-diff.html>
>> 
> 
> 
> 


> On 2 Dec 2020, at 15:29, Dimitre Novatchev <dnovatchev@gmail.com> wrote:
> 
> > slice($seq, -1)  to get the last item
> 
> > slice($seq, $N) to get the Nth item
> 
> > slice($seq, 1 to count($seq)-1) to get all but the last
> 
> -10  !
> 
> $seq[last()]
> 
> $seq[$n]
> 
> $seq[position() lt last()]
> 
> All of these are shorter than the proposed expressions containing slice() ! Why bother learn a new name and why pollute the function-name space?
> 
> This is just an example where we are going counter - Exupery and with close to no justification.
> 
> We also have another good friend: 
>   subsequence()
> 
> 
> Thanks,
> Dimitre
> 
> On Wed, Dec 2, 2020 at 3:16 AM Michael Kay <mike@saxonica.com <mailto:mike@saxonica.com>> wrote:
> The latest version allows
> 
> slice($seq, -1)  to get the last item
> 
> slice($seq, $N) to get the Nth item
> 
> slice($seq, 1 to count($seq)-1) to get all but the last
> 
> and I thought that was probably good enough to make the individual functions unnecessary.
> 
> Would you agree?
> 
> Michael Kay
> Saxonica
> 
>> On 2 Dec 2020, at 10:30, Christian Grün <cg@basex.org <mailto:cg@basex.org>> wrote:
>> 
>> Hi Michael,
>> 
>> In your first public XQFO proposal [1], I have found seen three
>> functions (fn:foot, fn:truncate, fn:item-at) that have not made it
>> into the latest specification documents. Are you still working on it,
>> or did you deliberately decide not to include them in the new draft?
>> 
>> Thanks,
>> Christian
>> 
>> [1] https://www.saxonica.com/qt4specs/FO/Overview-diff.html <https://www.saxonica.com/qt4specs/FO/Overview-diff.html>
>> 
> 
> 
> 

Received on Wednesday, 2 December 2020 17:29:39 UTC