Last active
August 29, 2015 14:24
-
-
Save kurtcagle/aed2d87641059d0be3ba to your computer and use it in GitHub Desktop.
Creating a deck of Cards in MarkLogic XQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module namespace cards = "http://semantical.co/ns/cards/"; | |
import module namespace stack = "http://semantical.co/ns/stack/" at "stack.xqy"; | |
declare function cards:new() as item() { | |
let $deck := | |
for $suit in ("♥","♣","♠","♦") | |
for $spot in ("A",(2 to 10),"J","Q","K") return | |
$spot || $suit | |
let $shuffled-deck := | |
for $item in $deck order by | |
xdmp:random(65535) return | |
$item | |
return stack:new($shuffled-deck) | |
}; | |
declare function cards:shuffle($deck as item()){ | |
let $stack := map:get($deck,"stack") | |
let $_ := map:put($deck,"stack", | |
for $item in $deck order by | |
xdmp:random(65535) return | |
$item | |
) | |
return $deck | |
}; | |
declare function cards:deal($deck as item(),$ct as xs:integer) as item()*{ | |
for $item in (1 to $ct) return stack:pop() | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment