Skip to content

Instantly share code, notes, and snippets.

@kurtcagle
Created July 12, 2015 21:43
Show Gist options
  • Save kurtcagle/3697029bf6601e48627d to your computer and use it in GitHub Desktop.
Save kurtcagle/3697029bf6601e48627d to your computer and use it in GitHub Desktop.
A stack is an XQuery map-based structure.
module namespace stack = "http://optum.com/ns/stack/";
declare function stack:new($seq as item()*) as map:map {
map:new((map:entry("stack",$seq)))
};
declare function stack:push($stack as map:map,$item as item()) {
map:put($stack,"stack",($item,map:get($stack,"stack")))
};
declare function stack:pop($stack as map:map) as item() {
let $stack-seq := map:get($stack,"stack")
let $head := fn:head($stack-seq)
let $_ := map:put($stack,"stack",fn:tail($stack-seq))
return $head
};
declare function stack:show($stack as map:map) as item()* {
map:get($stack,"stack")
};
declare function stack:draw($stack as map:map,$ct as xs:double) as item()* {
for $index in (1 to $ct) return stack:pop($stack)
};
declare function stack:peek($stack as map:map,$ct as xs:double) as item()* {
fn:subsequence(map:get($stack,"stack"),1,$ct)
};
declare function stack:clear($stack as map:map) as item()* {
let $stack-seq := stack:show($stack)
let $_ := map:put($stack,"stack",())
return $stack-seq
};
declare function stack:count($stack as map:map) as xs:long {
fn:count(map:get($stack,"stack"))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment