Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slicing (sequences and arrays) #22

Open
michaelhkay opened this issue Nov 14, 2020 · 7 comments
Open

Slicing (sequences and arrays) #22

michaelhkay opened this issue Nov 14, 2020 · 7 comments

Comments

@michaelhkay
Copy link
Member

I'm proposing an extension to range expressions so you can do

0 by 2 to 10 => 0,2,4,6,8,10
5 by -1 to 1 => 5,4,3,2,1

And then a slice() function so that

slice((A,B,C,D,E), 3 to 5) => C,D, E
slice((A,B,C,D,E), -1) => E
slice((A,B,C,D,E), 1 by 2 to 5) => A, C, E
slice((A,B,C,D,E), 5 by -1 to 2) => E, D, C, B
slice((A,B,C,D,E), -1 to 4) => E, A, B, C, D
slice((A,B,C,D,E), (1,5,4)) => A, E, D
@michaelhkay
Copy link
Member Author

michaelhkay commented Nov 14, 2020

And then a corresponding function array:slice().

For definitional purposes I'm wondering about defining operations like slice, subsequence, remove, reverse, insert-before in terms of corresponding operations on arrays, so fn:slice($seq, $range) is defined as array:slice(array{$seq}, $range)?*.s

A difficulty with this is the different conventions for handling "index out of bounds" conditions.

@benibela
Copy link

I'm proposing an extension to range expressions so you can do

0 by 2 to 10 => 0,2,4,6,8,10

That by could be defined as a filtering operator

  0 to 10 by 2 => (0 to 10) by 2 => 0,2,4,6,8,10

As such it could work with all sequences

  ("a", "b", "c", "d", "e") by 1 => ("a", "b", "c", "d", "e")
  ("a", "b", "c", "d", "e") by 2 => ("a", "c", "e")
  ("a", "b", "c", "d", "e") by 3 => ("a", "d")
  ("a", "b", "c", "d", "e") by 4 => ("a", "e")
  ("a", "b", "c", "d", "e") by 5 => ("a")

And then a slice() function so that

Or in the filter expression

 (A,B,C,D,E)[3, 4, 5] => C,D, E
 (A,B,C,D,E)[3 to 5] => C,D, E
 (A,B,C,D,E)[1 to 5 by 2 ] => A, C, E

the different conventions for handling "index out of bounds" conditions.

have been an extraordinary bad idea from the beginning

@michaelhkay
Copy link
Member Author

michaelhkay commented Nov 22, 2020 via email

@michaelhkay
Copy link
Member Author

michaelhkay commented Nov 22, 2020 via email

@rhdunn
Copy link

rhdunn commented Nov 22, 2020

I personally find 1 to 10 by 2 easier to read than 1 by 2 to 10, that is -- define the range, then the step. The former is also the way other languages like Scala define the ordering.

Other than that, I'm in favour of this extension.

@benibela
Copy link

That gets quite messy in edge cases especially where the expression used in the predicate is context-dependent, and can yield either a boolean or a number. It would be a natural if we didn't already have the difficult overloading of A[B] to do both filtering and subscripting.

I have argued for these things before

There I wrote A[B] could mean A[let $b := B return if (count($b) le 1 or head($b) instance of node()) then $b else position() = $b ]

Or perhaps A[ position() = B[. instance of xs:numeric] or B[not(. instance of xs:numeric)] ] would be more intuitive (but a bigger change, since it would treat [(1, <x/>)] as [true()])

You have to ask what expressions like //empl[1 to xs:integer(@height)] are supposed to mean. Or for that matter, //empl[1 to position()]. It might be doable, but the rules would certainly be complicated.

//empl[1 to position()] simply would return everything

//empl[1 to xs:integer(@height)] would probably be //empl[position() <= xs:integer(@height)]

@benibela
Copy link

(10 to 1) is an empty sequence, so ((10 to 1) by -1) would still be an empty sequence if "by" is a binary operator.

Perhaps by with negative number could be defined to reverse the sequence

Then 1 to 10 by -1 would be descending

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants