Skip to content

Instantly share code, notes, and snippets.

@raek
Created February 2, 2010 00:11
Show Gist options
  • Save raek/292210 to your computer and use it in GitHub Desktop.
Save raek/292210 to your computer and use it in GitHub Desktop.
(defn pred-get
"Steps through all the keys in pred-map, which are mutually exclusive
predicates, finds the first one that returns true when passed expr, and
returs the value associated with that predicate.
(map #(pred-get {neg? -1, zero? 0, pos? 1} %) [5 -2 0]) => (1 -1 0)"
[pred-map expr]
(loop [preds (keys pred-map)]
(when-first [pred preds]
(if (pred expr)
(get pred-map pred)
(recur (rest preds))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment