-
Notifications
You must be signed in to change notification settings - Fork 15
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
[XPath] Functions symmetric to head()
and tail()
for sequences and arrays
#97
Comments
+1. I proposed this function a while ago (I cannot find out where, though, apart from some remarks on Slack), and I remember that @michaelhkay preferred to name it |
PS: Analogous to |
And PPS (sorry, I’ll be more disciplined next time again) I agree with Dimitre, and I’d love to see |
I agree completely with Christian, and indeed, we need a |
I agree completely with Christian, and indeed, we need a fn:foot, because the existing fn:last doesn't do the symmetric of fn:head, as it is supposed to in other languages, such as Haskell. But the name My preferred names are: final, rear or ending. |
Related (quoting Michael Kay, https://app.slack.com/client/T011VK9115Z/C011NLXE4DU/thread/C011NLXE4DU-1605590306.224400):
|
. . . fn:rear($sequence as item()*) as item() Is a shorthand for: Examples:
It makes sense to have array:init($array as array(*)) as array(*)
array:rear($array as array(*)) as item()* Examples:
I would challenge anyone to re-write the last example in understandable way using |
My impression is that I agree that the syntax of |
:) A little humor isn't bad
Yes, and I updated the previous comment with examples of |
I updated the issue to include definition for |
head()
and tail()
for sequences and arrays
Is |
I understand the proposals but |
True. It's only the array functions that should trigger errors (and actually I never understood why array operations and functions were designed to raise range errors). |
I'm afraid there's always been tension between the error and no-error philosophies. Personally I tend to the "raise an error" camp - errors are easier to debug than wrong answers, and with XPath the hardest thing of all to debug is the expression that returns an empty sequence and you can't work out why. But the worst problem when you design by committee is that it's very hard to achieve a consistent policy on such things. |
My experience is mostly that it's confusing that sequence and array lookups behave differently. Maps are (fortunately?) similar to sequences: You won’t get errors if you request the value of a non-existing key. As a consequence, it's often easier to refactor existing sequence-based code into map-based code, although arrays would be the more natural choice. But, yes, it's difficult to evolve a language that has been very lax in the beginning, and I’m glad that the semantics of XQuery are stricter than the ones of XPath 1.0. |
@adamretter Exactly |
As an alternative to the name |
Interesting! In German, it’s »von Kopf bis Fuß« (from head to foot). |
I thought about "toe". But one has many toes, and just one head. Even for "foot", one has two feet, but just one head. Certainly, if there is such a saying, people may accept a function name as "toe" naturally. This is for the native English speakers to decide. |
But then, we have to rename the |
|
Are you advocating in favor of any of the terms presented in the image, @dnovatchev? It just came to me that even though it doesn’t meet Christian’s 4-letter criterion, And a So |
@gimsieke, Gerrit, |
Ok, |
@gimsieke |
In German, we can make the distinction by |
I think I found the definitive correct name for this function. The heel is the rearmost part of the foot, which is the farthest from the head Thus: fn:heel($sequence as item()*) as item() So, head() and heel() , that's it! And both start with an H and this is easier to remember. |
But it should be |
Note that |
I am not sure having And it would break the way people write recursion using head/tail, for instance, if That is just one example but it even appears in the (admittedly explanatory/optional) part of the functions and operators spec. |
Consistency should be the guiding rule here. The function should be symmetric with fn:head(). No errors. It also follows the principle in many standard functions that if the argument is an empty sequence, the result is an empty sequence. There's another principle here, which is that a function should be designed to work over the largest domain for which it is meaningful; errors should be reserved for cases where there's no conceivable use case for supplying a particular argument value. It would have made sense for |
Thanks @ChristianGruen. |
@michaelhkay, Obviously the people (and editor 😊) who put |
One last remark: |
Done! Also provided examples for this case. |
In #80 (comment), some more use cases are given for |
I don't know if there is still discussion about whether to use |
For selecting all items in a sequence except the last, I would vote for As regards the difference in error handling between arrays and sequences when a subscript is out of range, I think we have to live with it, and new functions should remain consistent. There are advantages and disadvantages to both designs, and it's a classic example of design-by-committee that different solutions have been adopted for the two cases. It's very difficult to change now for compatibility reasons. |
So I would prefer either the function name already in use in Haskell: |
Yes, Looking at |
I take your point about |
That's an excellent idea Norm:
|
Still, all other 3 names used here Maybe |
As far as I know, Haskell is the only language that uses |
I am against So, if this should have anything closer to "truncate", I definitely would prefer "truncated()" or "truncation()", or even "starting-segment()" or "left-segment()" |
Isn't that similar with If we wanted, we could check if other programming languages provide an ever better term. |
In another project, I settled for left/right with/of first/last to get subsequences.
It would match the functions like this:
|
If we didn't already have And if imperative verbs are allowed, then there can be no objection to |
Disagree. In this specific group of functions 3 of the 4 functions all have noun-names. So, what is so difficult to keep the naming of this group "clean" and find a last 4th noun that is suitable? And not to mention that we have quite a lot of native English speakers here ... |
Remember that we started with If we hadn't |
I'm not a native English speaker, but I have acquired a reasonable grasp of the language over the last 65 years, and I'm not aware of any noun that intuitively describes the thing that's left over when you remove its far end. You're thinking of a group of four functions as being in some way homogenous. I don't share that perspective. I see |
PR #250 has been raised. |
Closed by #250. |
In Xpath 3.1 we already have
head()
,tail()
, andlast()
But there is no function that produces the subsequence of all items of a sequence except the last one. There exists such a function in other programming languages. For example, in Haskell this is the init function.
And the
last()
function isn't the symmetric opposite ofhead()
-- it doesn't give us the last item in a sequence, just its position. So we need another function:fn:heel()
for this.init($seq)
is a convenient shorthand forsubsequence($seq, 1, count($seq) -1)
heel($seq)
is a convenient shorthand forslice($seq, -1)
Examples:
fn:init(('a', 'b', 'c'))
returns'a', 'b'
fn:init(('a', 'b'))
returns'a'
fn:init('a')
returns()
fn:init(())
returns()
fn:heel('a', 'b', 'c')
returns'c'
('a', 'b', 'c') => init() => heel()
returns'b'
It makes sense to have
fn:init()
andfn:heel()
defined on arrays, too.Examples:
array:init([1, 2, 3, 4, 5])
returns[1, 2, 3, 4]
array:init([1])
returns[]
array:heel([1, 2, 3, (4, 5)])
returns(4, 5)
array:heel([()])
returns()
(the empty sequence)array:init([])
produces errorarray:heel([])
produces error[1, 2, 3, (4, 5)] =>array:heel() => heel()
returns5
I would challenge anyone to re-write the last example in understandable way using
fn:slice()
💯The text was updated successfully, but these errors were encountered: