Skip to content

Instantly share code, notes, and snippets.

@MayDaniel
Created December 6, 2010 18:07
Show Gist options
  • Save MayDaniel/730659 to your computer and use it in GitHub Desktop.
Save MayDaniel/730659 to your computer and use it in GitHub Desktop.
(defmacro check-let [bindings & body]
(assert (vector? bindings))
(if (not-empty bindings)
(let [[binding [else & more]] (split-at 2 bindings)]
`(if-let [~@binding] (check-let [~@more] ~@body) ~else))
`(do ~@body)))
(check-let (a 1 ::else) ... ) ;; assert vector exception
(check-let [a] ... ) ;; if-let binding count exception
(check-let [a 1 ::else] 5) ;; 5
(check-let [a nil ::else] 5) ;; ::else
(check-let [foo (re-find #"foo" "foobar!") ::no-foo
bar (re-find #"\d+" "12345") ::no-d+]
(str foo bar))
;; => "foo12345"
(check-let [foo (re-find #"foo" "bazbar!") ::no-foo
bar (re-find #"\d+" "12345") ::no-d+]
(str foo bar))
;; => :user/no-foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment