comparison src/clojure/contrib/core.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
comparison
equal deleted inserted replaced
9:35cf337adfcf 10:ef7dbbd6452c
1 ; Copyright (c) Laurent Petit and others, March 2009. All rights reserved.
2
3 ; The use and distribution terms for this software are covered by the
4 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5 ; which can be found in the file epl-v10.html at the root of this
6 ; distribution.
7 ; By using this software in any fashion, you are agreeing to be bound by
8 ; the terms of this license.
9 ; You must not remove this notice, or any other, from this software.
10
11 ;; functions/macros variants of the ones that can be found in clojure.core
12
13 ;; note to other contrib members: feel free to add to this lib
14
15 (ns
16 ^{:author "Laurent Petit (and others)"
17 :doc "Functions/macros variants of the ones that can be found in clojure.core
18 (note to other contrib members: feel free to add to this lib)"}
19 clojure.contrib.core
20 (:use clojure.contrib.def))
21
22 (defmacro- defnilsafe [docstring non-safe-name nil-safe-name]
23 `(defmacro ~nil-safe-name ~docstring
24 {:arglists '([~'x ~'form] [~'x ~'form ~'& ~'forms])}
25 ([x# form#]
26 `(let [~'i# ~x#] (when-not (nil? ~'i#) (~'~non-safe-name ~'i# ~form#))))
27 ([x# form# & more#]
28 `(~'~nil-safe-name (~'~nil-safe-name ~x# ~form#) ~@more#))))
29
30 (defnilsafe
31 "Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
32 Examples :
33 (-?> \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
34 (-?> nil .toUpperCase (.substring 1)) returns nil
35 "
36 -> -?>)
37
38 (defnilsafe
39 "Same as clojure.core/.. but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
40 Examples :
41 (.?. \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
42 (.?. nil .toUpperCase (.substring 1)) returns nil
43 "
44 .. .?.)
45
46 (defnilsafe
47 "Same as clojure.core/->> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
48 Examples :
49 (-?>> (range 5) (map inc)) returns (1 2 3 4 5)
50 (-?>> [] seq (map inc)) returns nil
51 "
52 ->> -?>>)
53
54 ;; ----------------------------------------------------------------------
55 ;; scgilardi at gmail
56
57 (defn dissoc-in
58 "Dissociates an entry from a nested associative structure returning a new
59 nested structure. keys is a sequence of keys. Any empty maps that result
60 will not be present in the new structure."
61 [m [k & ks :as keys]]
62 (if ks
63 (if-let [nextmap (get m k)]
64 (let [newmap (dissoc-in nextmap ks)]
65 (if (seq newmap)
66 (assoc m k newmap)
67 (dissoc m k)))
68 m)
69 (dissoc m k)))
70
71 (defn new-by-name
72 "Constructs a Java object whose class is specified by a String."
73 [class-name & args]
74 (clojure.lang.Reflector/invokeConstructor
75 (clojure.lang.RT/classForName class-name)
76 (into-array Object args)))
77
78 (defn seqable?
79 "Returns true if (seq x) will succeed, false otherwise."
80 [x]
81 (or (seq? x)
82 (instance? clojure.lang.Seqable x)
83 (nil? x)
84 (instance? Iterable x)
85 (-> x .getClass .isArray)
86 (string? x)
87 (instance? java.util.Map x)))
88
89 ;; ----------------------------------------------------------------------