-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.clj
98 lines (89 loc) · 3.04 KB
/
utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(ns scenari.utils)
(defn contextual-eval [ctx expr]
(eval
`(let [~@(mapcat (fn [[k v]] [k `'~v]) ctx)]
~expr)))
(defmacro local-context []
(let [symbols (keys &env)]
(zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols)))
(defn readr [prompt exit-code]
(let [input (clojure.main/repl-read prompt exit-code)]
(if (= input ::tl)
exit-code
input)))
(defmacro break-with-repl []
`(clojure.main/repl
:prompt #(print "debug=> ")
:read readr
:eval (partial contextual-eval (local-context))))
(defn get-whole-in
""
([tree ks]
(get-whole-in tree ks nil))
([tree ks default]
(if (or (empty? ks) (nil? ks))
(if (empty? tree)
default
tree)
(if (keyword? (first tree))
;;mono node root tree
(if (= (first tree) (first ks))
(if (empty? (rest ks))
tree
(recur (rest tree) (rest ks) default))
default)
;;multi nodes root tree
(recur (mapcat vector
(filter (fn [node]
(if (keyword? (first node))
(= (first node) (first ks))
false)) tree))
(rest ks)
default)))))
(defn get-in-tree
""
([tree ks]
(get-in-tree tree ks nil))
([tree ks default]
(if (or (empty? ks) (nil? ks))
(if (empty? tree)
default
tree)
(if (keyword? (first tree))
;;mono node root tree
(if (= (first tree) (first ks))
(recur (rest tree) (rest ks) default)
default)
;;multi nodes root tree
(recur (mapcat (fn [node]
;;remove first element and add to the
(rest node))
(filter (fn [node]
(if (keyword? (first node))
(= (first node) (first ks))
false)) tree))
(rest ks)
default)))))
(defn color-str [color & xs]
(let [ansi-color #(format "\u001b[%sm"
(case % :reset "0" :black "30" :red "31"
:green "32" :yellow "33" :blue "34"
:purple "35" :cyan "36" :white "37" :grey "90"
"0"))]
(str (ansi-color color) (apply str xs) (ansi-color :reset))))
(def digits-only? (re-pattern #"^[0-9.]*$"))
(defn number-value-of
"given a string, detect if it contains digits only, then convert to a long or Double, otherwise return the string unchanged"
[s]
;;first a regex to detect if the string contains digits only
(let [re-number? (re-find digits-only? s)]
;;then try to cast the string to a number going from the largest type to the shortest one
(if re-number?
(try
(Long/valueOf s)
(catch java.lang.NumberFormatException nfe
(try
(Double/valueOf s)
(catch java.lang.NumberFormatException nfe
s))))
s)))