Skip to content

Instantly share code, notes, and snippets.

@tolitius
Last active December 12, 2016 17:33
Show Gist options
  • Save tolitius/f8f6155fe9646de79d7961cd99c0f33c to your computer and use it in GitHub Desktop.
Save tolitius/f8f6155fe9646de79d7961cd99c0f33c to your computer and use it in GitHub Desktop.
rules via lua's pcall
(defn pcall [f & args]
(try [true (apply f args)]
(catch Exception e
[false e])))
(defn matched? [[status value]]
(if status
(when (and value
(not= value :nginx-null))
value)
(println "ouch:" value)))
(defn nginx-null [& args]
:nginx-null)
(defn just-false [& args]
false)
(defn just-null [& args]
nil)
;; running it:
(matched? (pcall / 1 3))
;; => 1/3
(matched? (pcall nginx-null 1 0))
;; => nil
(matched? (pcall just-null 1 0))
;; => nil
(matched? (pcall just-false 1 0))
;; => nil
(matched? (pcall / 1 0))
;; => ouch: #error { :cause Divide by zero ... }
;; => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment