This feels like an ideal use case for XSLT for-each-group—something that is challenging to implement in XQuery.

 

Cheers,

 

E.

 

_____________________________________________

Eliot Kimber

Sr Staff Content Engineer

Digital Content & Design

O: 512 554 9368

M: 512 554 9368

servicenow.com

LinkedIn | Twitter | YouTube | Facebook

 

From: BaseX-Talk <basex-talk-bounces@mailman.uni-konstanz.de> on behalf of Graydon Saunders <graydonish@gmail.com>
Date: Thursday, March 28, 2024 at 7:43
AM
To: BaseX <BaseX-Talk@mailman.uni-konstanz.de>
Subject: [basex-talk] hof:until is gone?

[External Email]

 


Hello --

 

So I'm trying to do this:

 

import module namespace xcs = "http://www.xcential.com/xquery/utils/script" at "same-words-same-order-script.xqm";
(: we don't need xc computationally but there are external variables in that namespace in scope :)
import module namespace xc = "http://www.xcential.com/xquery/utils" at 'same-words-same-order.xqm';

declare function xc:dropTableLines($in as node()*,$toggle as xs:boolean) as node()* {
  switch (true())
    case empty($in) return ()
    case starts-with(head($in),':stab') return (<line/>,xc:dropTableLines(tail($in),true()))
    case starts-with(head($in),':rtab') return (<line/>,xc:dropTableLines(tail($in),false()))
    case $toggle return (<line/>,xc:dropTableLines(tail($in),$toggle))
    default return (head($in),xc:dropTableLines(tail($in),$toggle))
};

let $test as element() := <text>
  <line></line>
  <line></line>
  <line>:stab</line>
  <line>weasels</line>
  <line>:stab</line>
  <line>weasels</line>
  <line></line>
  <line></line>
  <line>:rtab.</line>
  <line></line>
  <line>asparagus</line>
  <line></line>
  <line></line>
  <line>:stab</line>
  <line></line>
  <line>weasels</line>
  <line>:rtab.</line>
  <line>asparagus</line>
  <line>asparagus</line>
  <line>asparagus</line>
  <line>asparagus</line>
  <line>asparagus</line>
  <line>:stab</line>
  <line>:stab</line>
  <line></line>
  <line></line>
  <line>:stab</line>
  <line></line>
  <line>weasels</line>
  <line></line>
  <line>:rtab.</line>
  <line></line>
  <line>asparagus</line>
  <line></line>
  <line></line>
  <line></line>
</text>

return element {'text'} {
            xc:dropTableLines($test/line,false())
          }

 

Only at scale the stack blows up and I get the "try tail recursion?" suggestion. I would have tried hof:until for that, since I have to pass the current state of "are we dropping or not dropping intervening content?", but it looks like it's been removed? And the available hof functions in 4 look like they're strictly positional which is actively unhelpful in this case. (At least with whatever brain cells I currently have.)

 

What's the appropriate pattern for "process a sequence, toggling an action on or off based on the last member of the sequence we looked at?"

 

Thanks!

Graydon