- From: <bugzilla@jessica.w3.org>
- Date: Tue, 23 Apr 2013 17:45:04 +0000
- To: public-qt-comments@w3.org
https://www.w3.org/Bugs/Public/show_bug.cgi?id=21797
Bug ID: 21797
Summary: Ordering of parameters in '16.2 Basic higher-order
functions'
Classification: Unclassified
Product: XPath / XQuery / XSLT
Version: Candidate Recommendation
Hardware: All
OS: All
Status: NEW
Severity: minor
Priority: P2
Component: Functions and Operators 3.0
Assignee: mike@saxonica.com
Reporter: adam@exist-db.org
QA Contact: public-qt-comments@w3.org
At present the following functions all apply a function to each item in a
sequence, which yields a new sequence (NOTE - fn:map-pairs applies the function
to two sequences).
fn:map
fn:filter
fn:fold-left
fn:fold-right
fn:map-pairs
However, the first argument is always the function at the moment. This is fine
for named functions, however for anonymous/inline functions this can perhaps
make your XQuery code harder to read. I propose moving the function argument
from the first argument of each of the above functions to the last argument.
Example 1, function argument first (current approach in spec.) -
map(function($x) {$x * 2 }, $sequence)
Example 2, function argument last (proposal) -
map($sequence, function($x) {$x * 2 })
There is not perhaps not too much discernible difference above, but as the
anonymous/inline functions become longer and more complex, it gets harder to
see what you are operating on using the current approach in the spec:
Example 3, function argument first (current approach in spec.) -
map(
function($pos as element(pos)) {
let $y := sin($pos/angle),
$dy := $y * $pos/angular-distance,
$all := ($dy, $known-distances),
$mean := avg($all),
$sq-diff := map(
function($x) {
let $res := $x - $mean
return $x * $x
},
$all
) return
mean($sq-diff)
},
$sequence
)
Example 4, function argument last (proposal) -
map($sequence, function($pos as element(pos)) {
let $y := sin($pos/angle),
$dy := $y * $pos/angular-distance,
$all := ($dy, $known-distances),
$mean := avg($all),
$sq-diff := map($all, function($x) {
let $res := $x - $mean return
$x * $x
}) return
mean($sq-diff)
})
Having the function argument last leads to a much more natural way of writing
code IMHO and also easier to read code. I can always see very quickly what I am
mapping because its the first argument in the map function. Also when using
anonymous functions in other functions, its easier to see where they finish and
similar to languages such as Scala and Javascript, because you have a final
"})"
--
You are receiving this mail because:
You are the QA Contact for the bug.
Received on Tuesday, 23 April 2013 17:45:06 UTC