-
-
Save amalloy/1346739 to your computer and use it in GitHub Desktop.
first-truthy-fn
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
;; this works fine | |
(defn- first-truthy-fn* [[preds & more-preds] args] | |
(when pred | |
(if (apply pred args) pred (recur more-preds args)))) | |
(defn first-truthy-fn | |
"Returns the first function in a seq of functions | |
that evaluates to truthy for the given arguments - | |
this shortciruits, only evaluating the functions | |
it needs to determine a result" | |
[preds & args] | |
(first-truthy-fn* preds args)) | |
;; what's wrong with this? | |
(defn first-truthy-fn [preds & args] | |
(loop [[p & more-ps] preds] | |
(when p | |
(if (apply p args) p (recur more-ps))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment