A question about the arrow operator and predicates

Wednesday, 31 March 2021 · 4 messages
CG
Christian Grünin reply to Bridger Dyson-Smith, 31 Mar 2021, 18:2831 Mar 2021 16:35 #

Dear Bridger,

Quoted text · Bridger Dyson-Smith · 5 lines
> I was wondering if anyone would have an insight for me as to why the following expression is wrong: > > for $s in ("/a/b/c", "/1/2/3") > let $t := $s => tokenize("/")[last()] > return $t

This is due to the grammar rules of XQuery 3.1, which mandate that “=>” is followed by an “ArrowFunctionSpecifier” and an “ArgumentList”. Here are some production rules from the spec:

[96] ArrowExpr ::= UnaryExpr ( "=>" ArrowFunctionSpecifier ArgumentList )*
[127] ArrowFunctionSpecifier ::= EQName | VarRef | ParenthesizedExpr
[122] ArgumentList ::= "(" (Argument ("," Argument)*)? ")"
…

The arrow function specifier can be an EQName, a variable reference or a parenthesized expression. The last one will do the job:

for $s in ("/a/b/c", "/1/2/3")
let $t := ($s => tokenize("/"))[last()]
return $t
Quoted text · Bridger Dyson-Smith · 18 lines
> > Thanks for your help! > Best, > Bridger > > PS I don't always remember to look at the optimized query in the Info window, but when I do I always get a hint about something; "util:last()" in this case. > > correctly > ``` > for $s in ("/a/b/c", "/1/2/3") > let $t := tokenize($s, "/")[last()] > return $t > ``` > ``` > for $s in ("/a/b/c", "/1/2/3") > let $t := $s => tokenize("/") => util:last() > return $t > ```
CG
Christian Grünin reply to Bridger Dyson-Smith, 31 Mar 2021, 18:5331 Mar 2021 16:59 #
Quoted text · Bridger Dyson-Smith · 3 lines
> > As always, thank you so much! >

As always, you're welcome!

Quoted text · Bridger Dyson-Smith · 4 lines
As a follow up, for my layman's thinking about XPath/XQuery, is this >> effectively creating a sequence > >

It does. The expression is completely equivalent to, and just another writing for: tokenize($s, "/").

BD
Bridger Dyson-Smith31 Mar 2021 18:28 #

Hi all -

I was wondering if anyone would have an insight for me as to why the following expression is wrong:

for $s in ("/a/b/c", "/1/2/3")
let $t := $s => tokenize("/")[last()]
return $t

This is because of the predicate filter, but I'm not clear on *why* it's because of that :)

Thanks for your help! Best, Bridger

PS I don't always remember to look at the optimized query in the Info window, but when I do I always get a hint about something; "util:last()" in this case.

correctly

for $s in ("/a/b/c", "/1/2/3")
let $t := tokenize($s, "/")[last()]
return $t

for $s in ("/a/b/c", "/1/2/3")
let $t := $s => tokenize("/") => util:last()
return $t
BD
Bridger Dyson-Smithin reply to Christian Grün, 31 Mar 2021, 16:3531 Mar 2021 18:53 #

Christian -

As always, thank you so much!

Quoted text · Christian Grün · 28 lines
On Wed, Mar 31, 2021 at 10:35 AM Christian Grün <christian.gruen at gmail.com> wrote: > Dear Bridger, > > > I was wondering if anyone would have an insight for me as to why the > following expression is wrong: > > > > for $s in ("/a/b/c", "/1/2/3") > > let $t := $s => tokenize("/")[last()] > > return $t > > This is due to the grammar rules of XQuery 3.1, which mandate that > “=>” is followed by an “ArrowFunctionSpecifier” and an “ArgumentList”. > Here are some production rules from the spec: > > [96] ArrowExpr ::= UnaryExpr ( "=>" ArrowFunctionSpecifier ArgumentList )* > [127] ArrowFunctionSpecifier ::= EQName | VarRef | ParenthesizedExpr > [122] ArgumentList ::= "(" (Argument ("," Argument)*)? ")" > … > > The arrow function specifier can be an EQName, a variable reference or > a parenthesized expression. The last one will do the job: > > for $s in ("/a/b/c", "/1/2/3") > let $t := ($s => tokenize("/"))[last()] > return $t > > It does - I had read some other online notes about parenthesizing part of

the left hand side expression, but hadn't parenthesized properly. As a follow up, for my layman's thinking about XPath/XQuery, is this effectively creating a sequence that is then filtered? Or am I mentally overloading the parentheses?

Thanks for the insight!

Hope this helps,

Quoted text · Christian Grün · 5 lines
> Christian > > [1] https://www.w3.org/TR/xquery-31/#id-arrow-operator > >

Best, Bridger

Quoted text · Christian Grün · 24 lines
> > > > > > > Thanks for your help! > > Best, > > Bridger > > > > PS I don't always remember to look at the optimized query in the Info > window, but when I do I always get a hint about something; "util:last()" in > this case. > > > > correctly > > ``` > > for $s in ("/a/b/c", "/1/2/3") > > let $t := tokenize($s, "/")[last()] > > return $t > > ``` > > ``` > > for $s in ("/a/b/c", "/1/2/3") > > let $t := $s => tokenize("/") => util:last() > > return $t > > ``` >

⚡Generated with XQuery