Re: Range Expressions

Yes, I think we should do what Python and Javascript do, and that rules out the idea of making "X by Y" a binary operator that can take "N to M" as the LHS. Which was a nice idea, but I think it fails: as you say, the results of (1 to 5 by -1) and (5 to 1 by -1) are too counter-intuitive.

My inclination now is to drop "by" as an operator keyword, and instead to have a function slice(seq, X, Y, Z) which behaves like seq[X:Y:Z] in Python and Javascript, subject to variations in edge cases (I think Python and Javascript differ only the handling of step=0). X, Y, and Z can all be defaulted.

This doesn't leave you with a simple way of expressing a sequence of integers such as (10 by 2 to 20) ; but most of the time when you want this, it's because you're going to use those integers for indexing some other sequence. If you really want it you can write slice(10 to 20, (), (), 2); or if we allow keyword arguments, slice(10 to 20, step: 2).

Alternatively, we could consider the syntax seq[X; Y; Z] as a more direct equivalent to the Javascript/Python slice operator. (But there's an objection to use of semicolon from people who want to reserve it for other purposes, such as scripting.) We could even consider seq[X: Y: Z], though colon is hazardous and needs whitespace in constructs like seq[start:end].  Another alternative would be use different brackets, for example seq[| X, Y, Z |]. But I'm inclined now to stick with slice() as a new function and steer clear of new syntax.


Michael Kay
Saxonica

> On 7 Dec 2020, at 14:42, Tomos Hillman <yamahito@gmail.com> wrote:
> 
> I think the most natural syntax would be to interpret the 'direction' of the step by the order of arguments; for me that is implicit by use of the word 'to': I would (naturally) expect:
> 
>  5 to 1 by 1 = (5, 4, 3, 2, 1)
>  1 to 5 by 1 = (1, 2, 3, 4, 5)
>  1 to -5 by 1 = (1, 0, -1, -2, -3, -4, -5)
>  1 to 5 by -1 = error
> 
> So that the 'by' argument is always positive.
> 
> Tom
> 
> _________________
> Tomos Hillman
> eXpertML Ltd
> +44 7793 242058
> On 7 Dec 2020, 14:19 +0000, Christian Grün <cg@basex.org>, wrote:
>> I showed the proposed syntax (1 to 5 by -1) to a non-X(Path|SLT|Query)
>> developer, and the immediate response was:
>> 
>> You must be wrong, it’s surely supposed to be: 5 to 1 by -1
>> 
>> I guess it’s too late to always start counting with the smaller value,
>> or to even allow "5 to 1" for reverse counts. Michael initially
>> proposed to place "by" ahead of "to" [1]…
>> 
>> 5 by -1 to 1
>> 
>> …and I wonder if it’s not the better choice?
>> 
>> [1] https://github.com/expath/xpath-ng/issues/22
>> ____________________________________
>> 
>> On Tue, Dec 1, 2020 at 10:02 PM Dimitre Novatchev <dnovatchev@gmail.com> wrote:
>>> 
>>>> Apart from anything else, it loses the idioms in the current language that rely on a range being a sequence of integers,
>>>> enabling familiar constructs such as x[position() = 1 to 10].
>>> 
>>> Well, there is nothing wrong with series of time periods in a total time range.
>>> 
>>> Maybe we can have a lifted overload of range, providing a function that, when applied on the provided as integers start, end and step, will produce the actual start, end and step for the actual range:
>>> 
>>> range($startBase as xs:integer,
>>> $endBase as xs:integer,
>>> $stepBase as xs:integer,
>>> $provider as function(xs:integer) as item() )
>>> as item()*
>>> 
>>> So, the provider() or better name, will convert the integers to whatever typed-data we need.
>>> 
>>> Thanks,
>>> Dimitre
>> 

Received on Monday, 7 December 2020 15:21:59 UTC