Skip to content

Instantly share code, notes, and snippets.

@txrev319
Created February 19, 2014 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save txrev319/9086538 to your computer and use it in GitHub Desktop.
Save txrev319/9086538 to your computer and use it in GitHub Desktop.
The following code
(defn do-stuff [msg]
(hmatch msg
[:dog age name] (println "got a dog with age " age " and name " name)
[:cat cute? legs] (println "got a cat that is " cute? " cute and has " legs "legs")
[other] (println "got an unrecognized animal of type " other ))
becomes:
(defn do-stuff [msg]
(case (tag :msg)
:dog (let [{:keys [age name]} msg] ...)
:cat (let [{:keys [cute? legs]} msg] ... )
(let [other (:tag msg)] ... )))
so basically, this "pattern matches" on objects of the form:
{:tag :dog
:age ...
:name ...}
and
{:tag :cat
:cute? ...
:legs ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment