[Bug 29869] New: Concise syntax for inline functions

https://www.w3.org/Bugs/Public/show_bug.cgi?id=29869

            Bug ID: 29869
           Summary: Concise syntax for inline functions
           Product: XPath / XQuery / XSLT
           Version: Candidate Recommendation
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Requirements for Future Versions
          Assignee: jim.melton@acm.org
          Reporter: mike@saxonica.com
        QA Contact: public-qt-comments@w3.org
  Target Milestone: ---

Inline functions in XQuery are verbose. Consider

fn:sort($employees, function($emp as element(employee)) { $emp/@salary })

Programming in a purely function style becomes a lot easier with a compact
syntax for writing inline functions. Compare:

XQuery (1)
function($x as xs:integer, $y as xs:integer) { $x + $y }

XQuery (2)
function($x, $y) {$x + $y}

Javascript 6
(x, y) => x+y

Java 8
(int x, int y) -> x+y

Scala (1)
(x: Int, y: Int) => x + y

Scala (2)
_ + _

Haskell
\ x y -> x + y

Python
lambda x, y: x + y


A couple of suggestions:

(a) Many use cases for simple inline functions take a single item as argument,
and we could exploit the existing use of "." for the context item for such
cases. For example use

\{. + 1} as shorthand for
function($x as item()){$x/(. + 1)}

allowing constructs such as

fn:sort($employees, \{@salary})

(b) For functions without declared types, we could implicitly declare the
arguments as $1, $2, etc:

\{$1 + $2}

allowing

fn:sort($employees, \{$1/@salary})

Or we could combine the two ideas with "." being a synonym for ($1 treat as
item()), thus

fn:sort($employees, \{@salary})

I'm not wedded to the backslash. Alternatives to \{$1+2} would be fn{$1+$2} or
{|$1+$2|} or even bare {$1+$2}.

-- 
You are receiving this mail because:
You are the QA Contact for the bug.

Received on Monday, 26 September 2016 06:31:58 UTC