[xquery-talk] arrow operator

Ghislain Fourny gfourny at inf.ethz.ch
Wed Aug 2 07:31:19 PDT 2017


Dear Wouter,

I see your point and it makes sense. The tricky part here, following up on your suggestion to make the choice of the first position less arbitrary, is that the context item must be an item: it cannot be a sequence of several items. This eliminates any design based on communicating the value on the left through the context item mechanism. The closest to the intent of => would be using a let variable binding.

The convention of binding the first argument, of course, implies that the (or some) functions are designed having in mind that the first parameter is “special”, that is, if the function is intended to be used with =>. In Java or C++, the self/this implied parameter is special, too (but it is not considered to be at any position).

Kind regards,
Ghislain


On 2 Aug 2017, at 16:22, W.S. Hager <wshager at gmail.com<mailto:wshager at gmail.com>> wrote:

To be precise, I think it means that the context needs to be bound to the result of the left side of the arrow, but perhaps that isn't even possible. Anyway, the convention seems to me just as trivial as binding the first argument.



2017-08-02 16:17 GMT+02:00 W.S. Hager <wshager at gmail.com<mailto:wshager at gmail.com>>:
Dear Ghislain,

This I understand, but the only thing that bothers me is that the implicit binding of the first argument prevents the use of other arguments, whereas an explicit context would've allowed for the more flexible option of designating the argument yourself, while the arity would read more clearly:

$position => array:remove($array, .)

This difference with the simple mapping operator is what confuses and surprises me, but it seems I'm the only one, so let's just leave it at that.

Thanks a lot for your time.

Wouter


2017-08-02 16:08 GMT+02:00 Ghislain Fourny <gfourny at inf.ethz.ch<mailto:gfourny at inf.ethz.ch>>:
Dear Wouter,

Indeed, in some cases, whether to use => or ! can be subject to a matter of taste.

To put things in perspective, => was introduced in version 3.1, together with map and array support. As far as I understand it and looking again at the specs, one of the use cases is for => to be used with maps and arrays, in an object-oriented style. The XPath and XQuery Functions and Operators 3.1 specification [1] explicitly gives a few examples:

["a", "b", "c"] => array:get(2)

$array => array:remove($position) => array:insert-before($position, $member)

Kind regards,
Ghislain

[1] https://www.w3.org/TR/xpath-functions-31/


> On 2 Aug 2017, at 12:09, W.S. Hager <wshager at gmail.com<mailto:wshager at gmail.com>> wrote:
>
> That's why, for cases where it's possible, I preferred to write the simple mapping operator, as it's easier to read IMO.
>
> 2017-08-02 12:07 GMT+02:00 W.S. Hager <wshager at gmail.com<mailto:wshager at gmail.com>>:
> Hi Ghislain,
>
> Alright, I forgot about fn:replace#4, but the implicit binding is sometimes hard to detect.
>
> Thanks.
>
>
> 2017-08-02 12:04 GMT+02:00 Ghislain Fourny <gfourny at inf.ethz.ch<mailto:gfourny at inf.ethz.ch>>:
> ... I didn't get it quite right actually. In @address => string(), there is no context item involved, I wrote too fast. :-)
>
> New attempt:
>
> @address ! string(.) (: explicitly passed context item :)
> @address ! string() (: context item passed implicitly to string#0, which is context-dependent)
> @address => string() (: @address passed implicitly to string#1 as the first parameter via the => operator, but string#1 is context-independent)
> @address => string(.) (: error: string#2 does not exist :)
>
> Kind regards,
> Ghislain
>
>
> > On 2 Aug 2017, at 12:00, Ghislain Fourny <gfourny at inf.ethz.ch<mailto:gfourny at inf.ethz.ch>> wrote:
> >
> > Dear Wouter,
> >
> > There is one more important difference on the syntactic level.
> >
> > With the arrow operator, the left-hand-side is implicitly bound to the first parameter of the function.
> >
> > @address => replace(@postcode, "", "q")
> >
> > is the same as
> >
> > replace(@address, @postcode, "", "q")
> >
> >
> > With the simple map operator, the context item must be explicitly referred to, like so:
> >
> > @address ! replace(., @postcode, "", "q")
> >
> >
> > What may create confusion is that some functions have several signatures, some of which implicitly refer to the context item. But this is a very different mechanism.
> >
> > For example :
> >
> > @address ! string(.) (: explicitly passed context item :)
> > @address ! string() (: context item passed implicitly to string#0, which is context-dependent)
> > @address => string() (: context item passed implicitly to string#1 via the => operator, but string#1 is context-independent)
> >
> > I hope I got it right!
> >
> > Kind regards,
> > Ghislain
> >
> >
> >> On 2 Aug 2017, at 11:27, W.S. Hager <wshager at gmail.com<mailto:wshager at gmail.com>> wrote:
> >>
> >> Hi Michael,
> >>
> >> The way you used the arrow operator in the example would be the way I expected it to work, namely by explicitly addressing the context, but it seems that it doesn't. It's actually implicitly binding the first argument of the function on the right to the value on the left. Or is there an exception I don't know about?
> >>
> >> Thanks.
> >>
> >> Op 1 aug. 2017 18:58 schreef "Michael Kay" <mike at saxonica.com<mailto:mike at saxonica.com>>:
> >> In the case of singletons there's very little difference, but (as I now see Christian has pointed out), with sequences the effect is quite different.
> >>
> >> Also, of course, "!" changes the context item, so
> >>
> >> @address => replace(@postcode, "", "q") works, while
> >>
> >> @address ! replace(@postcode, "", "q") doesn't.
> >>
> >> Michael Kay
> >> Saxonica
> >>
> >>> On 1 Aug 2017, at 13:27, W.S. Hager <wshager at gmail.com<mailto:wshager at gmail.com>> wrote:
> >>>
> >>> Hi,
> >>>
> >>> Is there any advantage to using the 3.1 arrow operator over the simple map operator?
> >>>
> >>> $string => upper-case() => normalize-unicode() => tokenize("\s+")
> >>>
> >>> versus
> >>>
> >>> $string ! upper-case(.) ! normalize-unicode(.) ! tokenize(.,"\s+")
> >>>
> >>> Thanks,
> >>> Wouter
> >>> _______________________________________________
> >>> talk at x-query.com<mailto:talk at x-query.com>
> >>> http://x-query.com/mailman/listinfo/talk
> >>
> >>
> >> _______________________________________________
> >> talk at x-query.com<mailto:talk at x-query.com>
> >> http://x-query.com/mailman/listinfo/talk
> >
>
>
>
>
>
>






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://x-query.com/pipermail/talk/attachments/20170802/af3a2256/attachment.html>


More information about the talk mailing list