Formal equivalents using fold-left

Many functions with callbacks, such as filter() and for-each() have a formal equivalent using fold-left(), which in turn is defined in terms of recursion.

Dropping the position argument from the fold-left callback means these need to be rewritten.

In most case it's easy enough, for example fn:filter becomes

fold-left($input ! {'item': ., 'pos': position()}, 
          (), 
          fn($result, $pair) {
            if ($predicate($pair?item, $pair?pos))
            then ($result, $pair?item)
            else $result
          })   

There's the slight disadvantage that this introduces a dependency on "!" and "position()", which are themselves specified rather informally.

Also, the same technique doesn't work for array functions like array:filter(), because we don't have a simple mapping operator for arrays.

If we want to reduce the number of primitives that we depend on, then defining numbered-items() (for sequences), and numbered-members() (for arrays) as primitives, using a recursive function for the formal equivalent, would seem to be the best way forward. The question then is whether to make these user-visible functions, or to use them for exposiition only,.

Michael Kay 

Received on Wednesday, 12 March 2025 09:57:26 UTC