Permalink
Browse files

add Scripting syntax description to README

  • Loading branch information...
sebbae committed Nov 8, 2016
1 parent 70444d5 commit 044df0168dc8b309cfa5c49c412fbd129a174a0c
Showing with 81 additions and 0 deletions.
  1. +81 −0 README.MD
View
@@ -205,3 +205,84 @@ let $r := {x : 1, y : 2, z : (3, 4) } return bit:values($r) (: yields the array
:)
let $s := io:read('/data/sample.json') return json:parse($s)
```
## Statement Syntax Extension (Beta)
**IMPORTANT NOTE:**
**
This extension is only a syntax extension to simplify programmer's life when writing XQuery. It is neither a subset of nor an equivalent to the XQuery Scripting Extension 1.0.
**
Almost any non-trivial data processing task consists of a series of consecutive steps. Unfortunately, the functional style of XQuery makes it a bit cumbersome to write code in a convenient, script-like fashion. Instead, the standard way to express a linear multi-step process (with access to intermediate results) is to write a FLWOR expression with a series of let-clauses.
As a shorthand, Brackit allows you to write such processes as a sequence of ';'-terminated statements, which most developers are familiar with:
```XQuery
(: declare external input :)
declare variable $file external;
(: read input data :)
$events := fn:collection('events');
(: join the two inputs :)
$incidents := for $e in $events
where $e/@severity = 'critical'
let $ip := x/system/@ip
group by $ip
order by count($e)
return {$ip} count($e) ;
(: store report to file :)
$report := {$incidents};
$output := bit:serialize($report);
io:write($file, $output);
(: return a short message as result :)
Generated '{count($incidents)}' incident entries to report '{$file}'
```
Internally, the compiler treats this as a FLWOR expression with let-bindings. The result, i.e., the return expression, is the result of the last statement. Accordingly, the previous example is equivalent to:
```XQuery
(: declare external input :)
declare variable $file external;
(: read input data :)
let $events := fn:collection('events')
(: join the two inputs :)
let $incidents := for $e in $events
where $e/@severity = 'critical'
let $ip := x/system/@ip
group by $ip
order by count($e)
return {$ip} count($e)
(: store report to file :)
let $report := {$incidents}
let $output := bit:serialize($report)
let $written := io:write($file, $output)
(: return a short message as result :)
return Generated '{count($incidents)}' incident entries to report '{$file}'
```
The statement syntax is especially helpful to improve readability of user-defined functions.
The following example shows an - admittedly rather slow - implementation of the quicksort algorithm:
```XQuery
declare function local:qsort($values) {
$len := count($values);
if ($len <= 1) then (
$values
) else (
$pivot := $values[$len idiv 2];
$less := $values[. < $pivot];
$greater := $values[. > $pivot];
(local:qsort($less), $pivot, local:qsort($greater))
}
};
local:qsort((7,8,4,5,6,9,3,2,0,1))
```

0 comments on commit 044df01

Please sign in to comment.