Skip to content

Instantly share code, notes, and snippets.

@lagenorhynque
Last active May 25, 2018 07:15
Show Gist options
  • Select an option

  • Save lagenorhynque/c71da21d47660fdd2f8b62b47a29cff4 to your computer and use it in GitHub Desktop.

Select an option

Save lagenorhynque/c71da21d47660fdd2f8b62b47a29cff4 to your computer and use it in GitHub Desktop.
;;; unless
user> (defmacro unless [cond then]
`(if (not ~cond)
~then))
#'user/unless
user> (macroexpand-1 '(unless foo? bar))
(if (clojure.core/not foo?) bar)
;;; case
user> (defmacro case-table [k m]
`(case ~k
~@(apply concat (eval m))))
#'user/case-table
user>
user> (def table (atom {}))
#'user/table
user> (swap! table assoc :foo '(hoge "foo"))
{:foo (hoge "foo")}
user> (swap! table assoc :bar '(hoge "bar!"))
{:foo (hoge "foo"), :bar (hoge "bar!")}
user> (defn hoge [s] ,,,)
#'user/hoge
user>
user> (defn fun1 [x]
(let [y (case-table x @table)]
,,,))
#'user/fun1
user> (macroexpand-1 '(case-table x @table))
(clojure.core/case x :foo (hoge "foo") :bar (hoge "bar!"))
user>
user> (swap! table dissoc :bar)
{:foo (hoge "foo")}
user> (swap! table assoc :baz '(hoge "baz!"))
{:foo (hoge "foo"), :baz (hoge "baz!")}
user>
user> (defn fun2 [x]
(let [y (case-table x @table)]
,,,))
#'user/fun2
user> (macroexpand-1 '(case-table x @table))
(clojure.core/case x :foo (hoge "foo") :baz (hoge "baz!"))
@lagenorhynque

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment