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