aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2009-02-26 23:57:06 +0000
committerscgilardi <scgilardi@gmail.com>2009-02-26 23:57:06 +0000
commitf47197d0b901b6cd1756212df262e0bb0b70605e (patch)
treebcdb288cf74dbf1661f3a50a305b91c8b2679eb7
parent99d4cec057e03f9da7331ef10d88e47ac9edfff2 (diff)
add map-utils from Jason Wolfe, fixes issue 10
-rw-r--r--src/clojure/contrib/map_utils.clj37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/clojure/contrib/map_utils.clj b/src/clojure/contrib/map_utils.clj
new file mode 100644
index 00000000..58ab03bc
--- /dev/null
+++ b/src/clojure/contrib/map_utils.clj
@@ -0,0 +1,37 @@
+;; Copyright (c) Jason Wolfe. All rights reserved. The use and
+;; distribution terms for this software are covered by the Eclipse Public
+;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
+;; be found in the file epl-v10.html at the root of this distribution. By
+;; using this software in any fashion, you are agreeing to be bound by the
+;; terms of this license. You must not remove this notice, or any other,
+;; from this software.
+;;
+;; map_utils.clj
+;;
+;; Utilities for operating on Clojure maps.
+;;
+;; jason at w01fe dot com
+;; Created 25 Feb 2009
+
+(ns clojure.contrib.map-utils)
+
+
+(defmacro lazy-get
+ "Like get, but doesn't evaluate not-found unless it is needed."
+ [map key not-found]
+ `(if-let [pair# (find ~map ~key)]
+ (val pair#)
+ ~not-found))
+
+(defn safe-get
+ "Like get, but throws an exception if the key is not found."
+ [map key]
+ (lazy-get map key
+ (throw (IllegalArgumentException. (format "Key %s not found in %s" key map)))))
+
+(defn safe-get-in
+ "Like get-in, but throws an exception if any key is not found."
+ [map ks]
+ (reduce safe-get map ks))
+
+