Last active
December 12, 2016 17:33
-
-
Save tolitius/f8f6155fe9646de79d7961cd99c0f33c to your computer and use it in GitHub Desktop.
rules via lua's pcall
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn nginx-null [& args] | |
:nginx-null) | |
(defn just-false [& args] | |
false) | |
(defn just-null [& args] | |
nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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