forked from dm3/clojure.java-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.clj
More file actions
52 lines (43 loc) · 1.69 KB
/
util.clj
File metadata and controls
52 lines (43 loc) · 1.69 KB
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
(ns java-time.util
(:require [clojure.string :as string])
(:import [java.lang.reflect Field]))
(defn get-static-fields-of-type [^Class klass, ^Class of-type]
(->> (seq (.getFields klass))
(map (fn [^Field f]
(when (.isAssignableFrom of-type (.getType f))
[(.getName f) (.get f nil)])) )
(keep identity)
(into {})))
(defn dashize [camelcase]
(let [words (re-seq #"([^A-Z]+|[A-Z]+[^A-Z]*)" camelcase)]
(string/join "-" (map (comp string/lower-case first) words))))
(defmacro if-threeten-extra [then-body else-body]
(if (try (Class/forName "org.threeten.extra.Temporals")
(catch Throwable e))
`(do ~then-body)
`(do ~else-body)))
(defmacro when-threeten-extra [& body]
(if (try (Class/forName "org.threeten.extra.Temporals")
(catch Throwable e))
`(do ~@body)))
(defmacro when-joda [& body]
(if (try (Class/forName "org.joda.time.DateTime")
(catch Throwable e))
`(do ~@body)))
;; From Medley, C Weavejester
(defn editable? [coll]
(instance? clojure.lang.IEditableCollection coll))
(defn reduce-map [f coll]
(if (editable? coll)
(persistent! (reduce-kv (f assoc!) (transient (empty coll)) coll))
(reduce-kv (f assoc) (empty coll) coll)))
(defn map-vals
"Maps a function over the values of an associative collection."
[f coll]
(reduce-map (fn [xf] (fn [m k v] (xf m k (f v)))) coll))
(defn map-kv
"Maps a function over the key/value pairs of an associate collection. Expects
a function that takes two arguments, the key and value, and returns the new
key and value as a collection of two elements."
[f coll]
(reduce-map (fn [xf] (fn [m k v] (let [[k v] (f k v)] (xf m k v)))) coll))