Search code examples
xquerymarklogicmarklogic-9

What does the operator "=>" in XQuery do?


When I look up XQuery examples i sometimes see the operator =>used.

I tried searching for the meaning of it, but could not find anything. Since i am using MarkLogic, it is possible that it is only for MarkLogic, but i doubt that.

From the examples i am aware that it somehow chains functions together, but i would like to know what is happening.

These are some examples i have found:

let $map := map:map()
  =>map:with("some-key",<info>45683</info>)
return <result>{$map}</result>
let $employees := op:from-view("main", "employees")
let $expenses  := op:from-view("main", "expenses")
let $totalexpenses  := op:col("totalexpenses")
return $employees
   => op:join-inner($expenses, op:on(
                    op:view-col("employees", "EmployeeID"),
                    op:view-col("expenses", "EmployeeID")))
   => op:group-by(op:view-col("employees", "EmployeeID"),
                 ("FirstName", "LastName", 
                  op:view-col("expenses", "Category"),
                  op:sum($totalexpenses, 
                  op:view-col("expenses", "Amount"))))
   => op:order-by(op:view-col("employees", "EmployeeID")) 
   => op:result() 

Solution

  • It is the Arrow Operator, which lets you supply the first argument to a function call from the outside.So if you have a function call foo($a, $b, $c), you can equivalently write it as $a => foo($b, $c). This is handy if you have lots of nested function calls as first arguments:

    string-join(reverse(tokenize(upper-case('a;b;c'), ';')), '_')
    

    With the arrow operator this can be written as a nice pipeline

    'a;b;c' => upper-case() => tokenize(';') => reverse() => string-join('_')
    

    giving the same result "C_B_A".

    One disadvantage of the arrow operator is that you have to take it into account when you want to find out at a glance which function is referenced by a function call in XQuery code. If you declared the two functions local:foo($seq) {...} and local:foo($seq, $accum) {...}, then $asdf => local:foo($x) looks like it calls the one-argument version, but actually calls the two-argument variant.