Skip to content

Instantly share code, notes, and snippets.

@takeoutweight
Last active August 29, 2015 14:00
Show Gist options
  • Save takeoutweight/14e2594120cd29f65860 to your computer and use it in GitHub Desktop.
Save takeoutweight/14e2594120cd29f65860 to your computer and use it in GitHub Desktop.
Macro expansion in match rules?
;match.clj 1764
(defn to-pattern-row
"Take an unprocessed pattern expression and an action expression and return
a pattern row of the processed pattern expression plus the action epxression."
[pat action]
(let [ps (map emit-pattern (map macroexpand (group-keywords pat)))]
(pattern-row ps action)))
(defmacro single-jeopardy [c-idx q-idx]
{:round :single-jeopardy
:category-idx c-idx
:question-idx q-idx})
(defmacro double-jeopardy [c-idx q-idx]
{:round :double-jeopardy
:category-idx c-idx
:question-idx q-idx})
(defmacro final-jeopardy []
{:round :final-jeopardy})
;eg:
(match [(regular-clue)]
[(single-jeopardy x y)] [:sing x y]
[(double-jeopardy cat q)] [:dub cat q]
[(final-jeopardy)] :final-jeopardy)
; or a :keys like destructuring
(defmacro rec-keys [klass & keys]
(list (into {} (map (fn [k] [(keyword k) k]) keys))
:guard `#(instance? ~klass %)))
(match [(regular-clue)]
[(rec-keys RegularClue category-idx)] category-idx
[(rec-keys FinalClue)] :final-jeopardy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment