Loading TOC...

MarkLogic 10 Product Documentation
xdmp:lazy

xdmp:lazy(
   $arg as item()*
) as item()*

Summary

Returns the value of its argument, evaluated lazily.

Parameters
arg The value to return

Usage Notes

This function does not change what result is returned; it only changes when the result is calculated. Eager evaluation performs all the work before the function returns. Lazy evaluation postpones the work to happen on demand as the result is consumed.

Lazy evaluation may be good idea if a result sequence is not consumed, or only partially consumed. For example, a long result sequence could be passed through fn:subsequence to only consume the first few items. Path expressions and cts functions that stream results are lazy by default for this reason.

Lazy evaluation is not a good idea if the result is consumed multiple times. Then the work is duplicated each time the result is consumed. Binding a result sequence to a variable and using the variable multiple times is consuming the result multiple times. In that case eager evaluation would be faster than lazy evaluation. If the streaming result of a path expression or cts function is used multiple times, xdmp:eager could make the code faster.

Example

let $eager := xdmp:eager(cts:search(fn:doc(), "hello"))
let $complex := xdmp:lazy(my:bigCalculation())
return
  if (fn:count($eager) > 10 )
  then $complex
  else ()

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