Loading TOC...

MarkLogic Server 11.0 Product Documentation
PreparePlan.prototype.reduce

PreparePlan.prototype.reduce(
   functionref as String,
   [seed as String]
) as IteratePlan

Summary

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 PreparePlan.prototype.reduce method can call prototype.map methods to chain map calls with reduce calls.

Parameters
functionref The function to be applied.
seed The value returned by the previous request.

Usage Notes

reduce is a method of the following classes:

Example

const op = require('/MarkLogic/optic');

const employees = op.fromView("main", "employees");

function fibReducer(previous, result) {
	  const i = Array.isArray(previous) ? previous.length : 0;

	  result.i = i;

	  switch(i) {
	  case 0:
	    result.fib = 0;
	    break;
	  case 1:
	    result.fib = 1;
	    break;
	  default:
	    result.fib = previous[i - 2].fib + previous[i - 1].fib;
	    break;
	  }

	  if (i === 0) {
	    previous = [result];
	  } else {
	    previous.push(result);
	  }

	  return previous;
	}

employees.orderBy('EmployeeID')
       .reduce(fibReducer)
       .result();

  

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