Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(ns spreads
(:use zeder))
(def legal-cards
[:๐Ÿ‚ก :๐Ÿ‚ฑ :๐Ÿƒ :๐Ÿƒ‘
:๐Ÿ‚ข :๐Ÿ‚ฒ :๐Ÿƒ‚ :๐Ÿƒ’
:๐Ÿ‚ฃ :๐Ÿ‚ณ :๐Ÿƒƒ :๐Ÿƒ“
:๐Ÿ‚ค :๐Ÿ‚ด :๐Ÿƒ„ :๐Ÿƒ”
:๐Ÿ‚ฅ :๐Ÿ‚ต :๐Ÿƒ… :๐Ÿƒ•
:๐Ÿ‚ฆ :๐Ÿ‚ถ :๐Ÿƒ† :๐Ÿƒ–
:๐Ÿ‚ง :๐Ÿ‚ท :๐Ÿƒ‡ :๐Ÿƒ—
:๐Ÿ‚จ :๐Ÿ‚ธ :๐Ÿƒˆ :๐Ÿƒ˜
:๐Ÿ‚ฉ :๐Ÿ‚น :๐Ÿƒ‰ :๐Ÿƒ™
:๐Ÿ‚ช :๐Ÿ‚บ :๐ŸƒŠ :๐Ÿƒš
:๐Ÿ‚ซ :๐Ÿ‚ป :๐Ÿƒ‹ :๐Ÿƒ›
:๐Ÿ‚ญ :๐Ÿ‚ฝ :๐Ÿƒ :๐Ÿƒ
:๐Ÿ‚ฎ :๐Ÿ‚พ :๐ŸƒŽ :๐Ÿƒž
])
(def goal [:๐Ÿƒž :๐Ÿƒˆ ])
(def S (engine :schemas #{{:z/ident :card-game/deck
:z/valueType :z.type/keyword
:z/cardinality :z.cardinality/many
:z/doc ""}
{:z/ident :card-game/goal
:z/valueType :z.type/keyword
:z/cardinality :z.cardinality/many
:z/doc ""}
{:z/ident :card-game/player
:z/valueType :z.type/string
:z/cardinality :z.cardinality/one
:z/doc ""}
{:z/ident :card-game/hand
:z/valueType :z.type/keyword
:z/cardinality :z.cardinality/many
:z/doc ""}}
:facts
(let [mf (gen-id)
ca (gen-id)]
#{[#z/id [_] :card-game/deck legal-cards]
[#z/id [_] :card-game/goal goal]
[mf :card-game/player "Fogus"]
[mf :card-game/hand goal]
[ca :card-game/player "Craig"]
[ca :card-game/hand [:๐Ÿ‚ฉ :๐Ÿ‚น :๐Ÿƒ‰ :๐Ÿƒ™ ]]})))
;; Spreads
;; =======
'[ca :card-game/hand [:๐Ÿ‚ฉ :๐Ÿ‚น :๐Ÿƒ‰ :๐Ÿƒ™ ]]
;; ^ ^
;; | |
;; +-- spread --+
;; Precise spread matches
;; ----------------------
(def exact-goal
'[(strict-winning-hands)
[?pid :card-game/player ?pname]
[?pid :card-game/hand [?card1 ?card2]]
[?gid :card-game/goal [?card1 ?card2]]])
;; ^ ^
;; | |
;; +-- spreads --+
(tuples->maps (q S exact-goal))
;;=> ({:z/id #uuid "40eb458b-af80-45b6-b08f-2f79f4fd7edf",
;; :card-game/player "Fogus",
;; :card-game/hand [:๐Ÿƒž :๐Ÿƒˆ]},
;; {:z/id #uuid "00000000-0000-07b7-0000-000000305ae3", :card-game/goal [:๐Ÿƒž :๐Ÿƒˆ]})
;; Front-loaded matches
;; --------------------
(def gif '[(front-winning-hands)
[?pid :card-game/player ?pname]
[?pid :card-game/hand [?card1 ?card2]]
[?gid :card-game/goal [?card1 ?card2 ...]]])
;; ^
;; |
;; ... two or more cards in hand
(let [rh (gen-id)]
(assert! S
[[rh :card-game/player "Rich"]
[rh :card-game/hand [:๐Ÿƒž :๐Ÿƒˆ :๐Ÿ‚ซ :๐Ÿ‚ป :๐Ÿƒ‹ :๐Ÿƒ› ]]]))
(tuples->maps (q S exact-goal))
;;=> ({:z/id #uuid "40eb458b-af80-45b6-b08f-2f79f4fd7edf",
;; :card-game/player "Fogus",
;; :card-game/hand [:๐Ÿƒž :๐Ÿƒˆ]},
;; {:z/id #uuid "00000000-0000-07b7-0000-000000305ae3", :card-game/goal [:๐Ÿƒž :๐Ÿƒˆ]})
(tuples->maps (q S gif))
;;=> ({:z/id #uuid "8d13960f-4276-408d-9a2d-89e013a8cc04",
;; :card-game/player "Fogus",
;; :card-game/hand [:๐Ÿƒž :๐Ÿƒˆ]},
;; {:z/id #uuid "00000000-0000-07b7-0000-000000305ae9", :card-game/goal [:๐Ÿƒž :๐Ÿƒˆ]},
;; {:z/id #uuid "99b4ef01-aac1-47db-8555-ad9e5a5ea079",
;; :card-game/player "Rich",
;; :card-game/hand [:๐Ÿƒž :๐Ÿƒˆ :๐Ÿ‚ซ :๐Ÿ‚ป :๐Ÿƒ‹ :๐Ÿƒ›]})
;; Anywhere matches
;; ----------------
(def anywhere '[(anywhere-winning-hands)
[?pid :card-game/player ?pname]
[?gid :card-game/goal [?card1 ?card2]]
[?pid :card-game/hand [... ?card1 ... ?card2 ...]]])
;;
;; * There are some number of cards, then a goal card, then more
;; cards, then another goal card and then more cards.
(let [jk (gen-id)]
(assert! S
[[jk :card-game/player "Jamie"]
[jk :card-game/hand [:๐Ÿ‚ซ :๐Ÿ‚ป :๐Ÿƒž :๐Ÿƒ‹ :๐Ÿƒˆ :๐Ÿƒ› ]]]))
(map :card-game/hand (tuples->maps (q S anywhere)))
;;=> ([:๐Ÿƒž :๐Ÿƒˆ] nil [:๐Ÿƒž :๐Ÿƒˆ :๐Ÿ‚ซ :๐Ÿ‚ป :๐Ÿƒ‹ :๐Ÿƒ›] [:๐Ÿƒž :๐Ÿ‚ซ :๐Ÿ‚ป :๐Ÿƒ‹ :๐Ÿƒˆ :๐Ÿƒ›])
;; ### But...
;; ... spreads are ordered
'[(uber-anywhere-winning-hands)
[?pid :card-game/player ?pname]
[?pid :card-game/hand [?card1 ?card2]]
[?gid :card-game/goal [... ?card2 ... ?card1 ...]]]
;; ^ ^
;; | |
;; +----------+
;; |
;; cards and goals should be
;; pre-ordered and insertion
;; sorted for nice matching with
;; spreads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment