-
Notifications
You must be signed in to change notification settings - Fork 4
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 (3 options) for concise inline function syntax #5
base: master
Are you sure you want to change the base?
Conversation
Proposes various options for simplified syntax for declaring inline functions, and makes recommendations
The text for this is a copy of #3. |
(Entire document replaced; previous version was the wrong document)
Hopefully corrected now. I'm still getting used to this way of working. |
I would clearly be in favor of having options of this proposal accepted. My thoughts on… Option 3If we started from scratch, this would probably be the clear winner: Users can name their variables as they like, and the syntax is well-established. I expected that the necessity of the unbounded look-ahead would make it a clear no-go. It’s good to have your assessment; maybe I’ll give it a try and gain some experience. Option 2One criticism on this approach could be that it is not as general-purpose as some users may believe at first sight. If the last parameter of a function is not referenced, the code cannot be refactored: Example: declare function local:inc-filter($numbers, $filter) {
for $n in $numbers
let $i := $n + 1
where $filter($i)
return $i
};
local:inc-filter((0 to 3), function($n) { boolean($n mod 2) }),
local:inc-filter((0 to 3), function($n) { true() }) Rewritten function calls: local:inc-filter((0 to 3), -> boolean($1 mod 2),
local:inc-filter((0 to 3), function($n) { true() }) (: no rewriting possible :) On the other hand, in some cases, this may indicate to a user that there is a better way of writing the original code. Apart from this restriction, I really like the option: It’s even more concise than Option 3, and… Option 1…I would appreciate if we could use the same syntax for focus functions. |
In general I support the idea of a simpler syntax for inline functions. I am still chewing through the detail of the proposal but I have a comments and questions: Focus Functions
Short Inline Functions
Syntax with Declared Parameters
I did a quick look at the languages I use the most (and a couple that I respect) to see how their syntax represents such things: Java
Scala
C++
Slightly different to others! You have to specify any Haskell
A nice piece of trivia, the Lisp
Rust
I understand that you can also place the characters |
Thanks for the detailed comments. Some observations:
While forming these ideas I did consider mechanisms from other languages (though my survey wasn't as wide as yours). The idea of "focus functions" is influenced by the ability to write Another thing I looked at is dropping the "->" prefix, so we recognize |
Hmm, I can see your point here. I don't think I am communicating my ideas very well on this issue. Given the example: filter(//employee, ->@salary gt 20000) I can see that the thing on the RHS is going to be evaluated once for each thing on the LHS. Perhaps It also reminds very much of the Simple Map Operator... How bad would a syntax like |
Another language is Kotlin
If the parameters are omitted, it uses an implicit default parameter That syntax would work well in XQuery (unless |
Lately I have been writing a lot of nested anonymous functions for a research paper. I realised that there may be a shortcoming with the "Short Inline Functions" form. How would we handle nested inline functions? An example which is a function that both takes a function and returns a function. I wonder what this would look like when rewritten as "Short Inline Functions": declare function other:something($f as function(xs:string, xs:integer) as function(xs:QName, xs:QName) as xs:string) as xs:integer external;
other:something(function($a, $b) {
function($c, $d) {
"place holder"
}
}); |
We have a syntax that works for big complicated functions, we don't have one that works for trivial inline functions like filter($seq, {a gt 2}).
The syntax for the trivial inline functions needs to be unambiguous, and it needs to be very usable for little functions, but it doesn't need to be have high usability for complex cases.
The "." variable itself is optimized for simple cases; with only one variable, you can't do joins. That was a design choice that kept XPath simple and accounted for a lot of its success. Optimize for simple and common cases.
Michael Kay
Saxonica
… On 16 Jan 2019, at 03:03, Adam Retter ***@***.***> wrote:
Lately I have been writing a lot of nested anonymous functions for a research paper.
I realised that there may be a shortcoming with the "Short Inline Functions" form. How would we handle nested inline functions?
An example which is a function that both takes a function and returns a function. I wonder what this would look like when rewritten as "Short Inline Functions":
declare function other:something($f as function(xs:string, xs:integer) as function(xs:QName, xs:QName) as xs:string) as xs:integer external;
other:something(function($a, $b) {
function($c, $d) {
"place holder"
}
});
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#5 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ACSIIt_FRDLLC_GprfbiMz_FTHPeJT39ks5vDpZygaJpZM4Xd6nV>.
|
I still like the 2 proposed options a lot. Option 3 would surely be the most general one. It seems we can handle it in our implementation, but I don’t know if that will be true for everyone else who might be interested in supporting this feature in the future? |
@michaelhkay Okay understood, and I agree. I wasn't trying to criticise, rather I was trying to understand if there was a clever way it could be achieved with the "Short Inline Functions" form, that I hadn't understood... I guess not. |
@ChristianGruen I would certainly be interested in whatever options we can agree on :-) |
I like the idea of using the Kotlin-style syntax as a variant of option 3, replacing Simple case (both equivalent):
Implicit context item -- single parameter function:
Explicit (named) context item -- single parameter function:
Multi-parameter function:
Combined with sequence, map, and array decomposition:
Multiple parameters combined with sequence, map, and array decomposition:
|
There is of course competition for the scarce resource of bare-brace expressions. Other contenders include:
* map syntax, a la Javascript
* execution blocks in XQuery scripting
and there's an argument against using it at all, because of confusion with the use of braces in AVTs
It's because of this competition that we've always prefixed "{" with something else, e.g. in EQName syntax and in map syntax.
Michael Kay
Saxonica
… On 16 Jan 2019, at 12:12, Reece H. Dunn ***@***.***> wrote:
I like the idea of using the Kotlin-style syntax as a variant of option 3, replacing it with .. This resolves the issue of unbounded lookup, as the concise function is initiated by a {. This is also unambiguous in the current syntax as {...} is only allowed in direct element content, and other uses require a type indicator (such as array { ... }).
Simple case (both equivalent):
{ 1 }
{ -> 1 }
Implicit context item -- single parameter function:
sort(employee, { @salary })
{ . * 2 }
{ -> . * 2 }
Explicit (named) context item -- single parameter function:
sort(employee, { $e -> @salary })
{ $n -> $n * 2 }
Multi-parameter function:
{ $k, $v -> $k }
{ ($k, $v) -> $k }
{ $k as xs:string, $v -> $k }
Combined with sequence, map, and array decomposition:
{ ${key, value} -> $k }
{ $[key, value] -> $k }
{ $(key, value) -> $k }
Multiple parameters combined with sequence, map, and array decomposition:
{ $x, ${key, value} -> $k }
{ ($x, $(key, value)) -> $k }
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#5 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ACSIImHWNpftWhi1Ica9ll4gPMQgJU3cks5vDxcygaJpZM4Xd6nV>.
|
Proposes various options for simplified syntax for declaring inline
functions, and makes recommendations