7

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() 

1 Answers1

9

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.

Leo Wörteler
  • 4,191
  • 13
  • 10
  • 4
    It's also easily confused with the bang operator, e.g. `//x ! upper-case(.)`. The difference is that (a) The bang operator applies the RHS to every item in the sequence on the LHS, and (b) you can't omit the argument: note the `upper-case(.)`. Also (c) the bang operator can have any expression on the RHS, the arrow operator can only have a function call. – Michael Kay May 13 '20 at 15:14
  • Some may recognize it as method chaining from Fluent programming. Keep in mind that when chaining, you continue with the result, so custom chaining functions should return something that can be chained. Optic functions are designed for this. They all return a changed plan that can be used for chaining. It would also work great on the admin functions of MarkLogic Server. – grtjn May 14 '20 at 07:01