/
core_ext.clj
75 lines (62 loc) · 2.29 KB
/
core_ext.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(ns kaocha.core-ext
"Core language extensions"
(:refer-clojure :exclude [symbol])
(:import [java.util.regex Pattern]))
(defn regex? [x]
(instance? Pattern x))
(defn exception? [x]
(instance? java.lang.Exception x))
(defn error? [x]
(instance? java.lang.Error x))
(defn throwable? [x]
(instance? java.lang.Throwable x))
(defn ns? [x]
(instance? clojure.lang.Namespace x))
(defn file? [x]
(and (instance? java.io.File x) (.isFile ^java.io.File x)))
(defn directory? [x]
(and (instance? java.io.File x) (.isDirectory ^java.io.File x)))
(defn path? [x]
(instance? java.nio.file.Path x))
(defn regex
([x & xs]
(regex (apply str x xs)))
([x]
(cond
(regex? x) x
(string? x) (Pattern/compile x)
:else (throw (ex-info (str "Can't coerce " (class x) " to regex.") {})))))
(defn mapply
"Applies a function f to the argument list formed by concatenating
everything but the last element of args with the last element of
args. This is useful for applying a function that accepts keyword
arguments to a map."
[f & args]
(apply f (apply concat (butlast args) (last args))))
(defn symbol
"Backport from Clojure 1.10, symbol function that's a bit more lenient on its
inputs.
Returns a Symbol with the given namespace and name. Arity-1 works on strings,
keywords, and vars."
^clojure.lang.Symbol
([name]
(cond
(symbol? name) name
(instance? String name) (clojure.lang.Symbol/intern name)
(instance? clojure.lang.Var name) (.toSymbol ^clojure.lang.Var name)
(instance? clojure.lang.Keyword name) (.sym ^clojure.lang.Keyword name)
:else (throw (IllegalArgumentException. "no conversion to symbol"))))
([ns name] (clojure.lang.Symbol/intern ns name)))
;; 1.10 backport
(when-not (resolve 'clojure.core/requiring-resolve)
;; using defn generates a warning even when not evaluated
(intern *ns*
^{:doc "Resolves namespace-qualified sym per 'resolve'. If initial resolve
fails, attempts to require sym's namespace and retries."}
'requiring-resolve
(fn [sym]
(if (qualified-symbol? sym)
(or (resolve sym)
(do (-> sym namespace symbol require)
(resolve sym)))
(throw (IllegalArgumentException. (str "Not a qualified symbol: " sym)))))))