-
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
Proposal: Add the missing functions for arrays: array:exists() and array:empty() #229
Comments
I worry a little about the name array:exists(), it feels too much like a test as to whether the array exists, not whether it has any members. |
I always felt |
It's unfortunate that we have an inconsistency between sequences, arrays, and maps: we use For sequences, there's no confusion about what "not existing" means, but I see Mike's point about "exists" functions on maps and arrays. I suppose as long as we don't allow the argument to It does strike me as slightly disconcerting that if you wanted to know if an optional array (or map) was non-empty, you'd have to write:
But the alternative of allowing the argument to On balance, I'm in favor. They work like OTOH, I don't find |
Compatibility means deliberately repeating other people's mistakes. Yes, I think |
I suppose
Whether that's a good idea or not... |
+1 to latest suggestion by @ndw with some suggested alternative names for discussion (because I think
|
In LINQ (C#) any IEnumerable has the method Any() There is no equivalent for Thus I would prefer something similar (in decreasing order of preference):
For implementation I would strongly recommend not to mention |
Perhaps
The implementation does not need to calculate the size to test |
Definitely not. The array has one member. And this empty sequence is the actual data contained in that member. An empty sequence is in fact actual data. For example, the empty sequence can be the value passed as argument in a given function call.
Yes, and given that some of the comments above referred to |
These would be useful functions! I agree that I prefer data-type specific terminology like
I'd also welcome a corresponding
And for the |
@joewiz ,
👍
👍
Let us not mention I think that this problem is eliminated using the following equivalent expressions, instead:
|
I like Most existing built-in functions have names without auxiliary verbs, indicating possession ( I think we don’t additionally need |
Ignoring other questions, there's nothing wrong with the specification being defined in terms of a very inefficient implementation. We do that all the time. It's a specification of what result the function delivers, it's not guidance for the implementor on how to implement it efficiently. |
@michaelhkay , Even so, maybe it is always a good idea putting a warning that literally having such implementation could be ridiculously inefficient, right? @dnovatchev In general, I don't think implementors expect the specification writers to give them advice on how to implement the specification efficiently. We do so occasionally, e.g. in the Note at the end of XSLT 3.0 section 5.5.3 (see https://www.w3.org/TR/xslt-30/#pattern-semantics) but if we did this too often then (a) it would get very tedious, and (b) we would often get the advice wrong, since implementors are sometimes much smarter than we imagine. |
I agree with Michael: I see no need to add hints for implementors to the spec. There are numerous other language features that would be very inefficient if implemented without further thoughts (think of e.g. |
Yes, this is true, but we can at least put a short reminder in parenthesis, something like: (note, this can be implemented with O(1) time complexity) @dnovatchev I can envisage implementations of arrays in which determining whether the array is empty involves looking to see how many members are marked as deleted/removed. That will not be O(1). We might think that's not a good implementation, but it's not our call. The implementor might want to reduce the cost of remove() operations at the expense of increasing the cost of is-empty() - that's entirely up to them, it's not for us to judge. It's perfectly adequate to say that |
If the specification says (as suggested in your comment:
It would be good also to add: "but this is not a suggestion to use |
@dnovatchev All these comments could certainly be added, but to be honest, I don't understand who would benefit from such notes. We should find at least one implementor out there who believes this would be of help. |
@ChristianGruen We could have such statement just in a single, central section of the specification, just to be known that the provided "possible implementations code" is only for instructive, definitional purposes and should not be perceived as any recommendation to implement exactly this code/algorithm. When distinguishing between "clear" and "useful", "clear" comes before "useful" (because if something is unclear, we cannot judge whether it is useful or not) |
So we should possibly find at least one implementor who believes that the specification gets more clear if hints of that kind are added. |
The initially proposed functions have been added to the spec (#250). |
The functions over sequences:
fn:exists
andfn:empty
are amongst the most useful and well-understood sequence functions.In case we need to know whether or not an array has at least one member, or none, these function cannot help us:
To fill this gap, these two functions are proposed:
array:exists
:Signature
Properties
This function is ·deterministic·, ·context-independent·, and ·focus-independent·.
Rules
If the value of
$arg
is a non-empty array, the function returnstrue()
; otherwise, the function returnsfalse()
.Examples
The expression
array:exists(array:remove(["hello"], 1)
returnsfalse()
.The expression
aray:exists(array:remove(["hello", "world"], 1)
returnstrue()
.The expression
aray:exists([])
returnsfalse()
.The expression
aray:exists([ () ])
returnstrue()
.array:empty
:Signature
Properties
This function is ·deterministic·, ·context-independent·, and ·focus-independent·.
Rules
If the value of
$arg
is the empty array, the function returnstrue()
; otherwise, the function returnsfalse()
.Examples
The expression
array:empty(array:remove(["hello"], 1)
returnstrue()
.The expression
aray:empty(array:remove(["hello", "world"], 1)
returnsfalse()
.The expression
aray:empty([])
returnstrue()
.The expression
aray:empty([ () ])
returnsfalse()
.The text was updated successfully, but these errors were encountered: