Skip to content

Instantly share code, notes, and snippets.

@tyrion
Created January 31, 2015 17:40
Show Gist options
  • Save tyrion/36bd249c01e2589f32d2 to your computer and use it in GitHub Desktop.
Save tyrion/36bd249c01e2589f32d2 to your computer and use it in GitHub Desktop.
Madness of XQuery
declare function local:maxconsecutive($partite as element()*, $squadra as xs:string, $i as xs:integer, $max as xs:integer, $tot as xs:integer) as xs:integer {
(if ($i = count($partite)+1) then ( local:max($max, $tot))
else (
if (local:vinta($partite[$i], $squadra))
then (local:maxconsecutive($partite, $squadra, $i+1, $max+1, $tot))
else (local:maxconsecutive($partite, $squadra, $i+1, 0, local:max($max, $tot)))
))
};
declare function local:max($a as xs:integer, $b as xs:integer) as xs:integer {
(if ($a >= $b) then ($a) else ($b))
};
declare function local:vinta($partita, $squadra) as xs:boolean {
(($partita/squadracasa = $squadra and $partita/goalcasa > $partita/goalospite) or
($partita/squadraospite = $squadra and $partita/goalcasa < $partita/goalospite))
};
let $partite := doc("partite.xml")//partita
return local:maxconsecutive($partite, string("roma"), 1, 0, 0)
@tyrion
Copy link
Author

tyrion commented Jan 31, 2015

Recursion FTW!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment