BaseXdb / basex

BaseX Main Repository.
http://basex.org
BSD 3-Clause "New" or "Revised" License
661 stars 267 forks source link

XQuery, fold: early exit #2244

Closed ChristianGruen closed 10 months ago

ChristianGruen commented 10 months ago

If the computed result in a fold function does not change anymore, the remaining computations can be skipped. Examples:

fold-left($huge, 1, fn($result, $item) { true() })
fold-left($huge, 1, fn($result, $item) { $result })
fold-left($huge, 1, fn($result, $item) { if($result > 100) then $result else $result + $item })
fold-left($huge, 1, fn($result, $item) { if($result < 100) then $result + $item else $result })

The same applies to fold-right, array:fold-left and array:fold-right.