Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 86 lines (75 sloc) 3.125 kb
33b04a0c »
2011-05-04 Pulling in c.c.core and defmacro- from c.c.def
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 (ns ^{:author "Laurent Petit (and others)"
12 :doc "Functions/macros variants of the ones that can be found in clojure.core
13 (note to other contrib members: feel free to add to this lib)"}
14 clojure.core.incubator)
15
16 (defmacro defmacro-
17 "Same as defmacro but yields a private definition"
18 [name & decls]
19 (list* `defmacro (with-meta name (assoc (meta name) :private true)) decls))
20
21 (defmacro- defnilsafe [docstring non-safe-name nil-safe-name]
22 `(defmacro ~nil-safe-name ~docstring
23 {:arglists '([~'x ~'form] [~'x ~'form ~'& ~'forms])}
24 ([x# form#]
25 `(let [~'i# ~x#] (when-not (nil? ~'i#) (~'~non-safe-name ~'i# ~form#))))
26 ([x# form# & more#]
27 `(~'~nil-safe-name (~'~nil-safe-name ~x# ~form#) ~@more#))))
28
29 (defnilsafe
30 "Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
31 Examples :
32 (-?> \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
33 (-?> nil .toUpperCase (.substring 1)) returns nil
34 "
35 -> -?>)
36
37 (defnilsafe
38 "Same as clojure.core/.. but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
39 Examples :
40 (.?. \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
41 (.?. nil .toUpperCase (.substring 1)) returns nil
42 "
43 .. .?.)
44
45 (defnilsafe
46 "Same as clojure.core/->> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
47 Examples :
48 (-?>> (range 5) (map inc)) returns (1 2 3 4 5)
49 (-?>> [] seq (map inc)) returns nil
50 "
51 ->> -?>>)
52
53 ;; ----------------------------------------------------------------------
54 ;; scgilardi at gmail
55
56 (defn dissoc-in
57 "Dissociates an entry from a nested associative structure returning a new
58 nested structure. keys is a sequence of keys. Any empty maps that result
59 will not be present in the new structure."
60 [m [k & ks :as keys]]
61 (if ks
62 (if-let [nextmap (get m k)]
63 (let [newmap (dissoc-in nextmap ks)]
64 (if (seq newmap)
65 (assoc m k newmap)
66 (dissoc m k)))
67 m)
68 (dissoc m k)))
69
70 (defn new-by-name
71 "Constructs a Java object whose class is specified by a String."
72 [class-name & args]
73 (clojure.lang.Reflector/invokeConstructor
74 (clojure.lang.RT/classForName class-name)
75 (into-array Object args)))
76
77 (defn seqable?
78 "Returns true if (seq x) will succeed, false otherwise."
79 [x]
80 (or (seq? x)
81 (instance? clojure.lang.Seqable x)
82 (nil? x)
83 (instance? Iterable x)
84 (-> x .getClass .isArray)
85 (string? x)
86 (instance? java.util.Map x)))
Something went wrong with that request. Please try again.