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.)