Loading TOC...


   $plan as map:map,
   $reducer as function(item()*, item()) as item()*,
   [$seed as item()*]
) as map:map


This method applies a function or the builtin reducer to each row returned by the plan to produce a single result as with the reduce() method of JavaScript Array.

The signature of the reducer must be function(previous, row), where previous is the seed on the first request and the return from the previous call on subsequent request and row is the current row.

The implementation of a op:reduce function can call op:map functions to chain map calls with reduce calls.

$plan The Optic Plan. You can either use the XQuery => chaining operator or specify the variable that captures the return value from the previous operation.
$reducer The function to be applied.
$seed The value returned by the previous request.


xquery version "1.0-ml";

import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $employees := op:from-view("main", "employees")

return $employees
   => op:select("EmployeeID")
   => op:order-by("EmployeeID")
   => op:reduce(function($previous as json:array, $row as map:map) as json:array {
	       let $i := json:array-size($previous)
	           return json:array-with(
	               map:with($row, "foo",  $i) => map:with("bar",
	                      case 0  return 0
	                      case 1  return 1
	                      default return map:get($previous[$i - 1], "bar") + map:get($previous[$i], "bar"))
   => op:result("value")


Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.