-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.clj
58 lines (51 loc) · 1.59 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
; This Source Code Form is subject to the terms of the Mozilla Public
; License, v. 2.0. If a copy of the MPL was not distributed with this
; file, You can obtain one at https://mozilla.org/MPL/2.0/.
(ns noahtheduke.splint.utils)
(set! *warn-on-reflection* true)
(defn drop-quote
"Convert (quote (a b c)) to (a b c)."
[sexp]
(if (and (seq? sexp)
(= 'quote (first sexp)))
(fnext sexp)
sexp))
(defprotocol SexpType
(simple-type
[sexp]
"Because Clojure doesn't have this built-in, we must do it the slow way: take
an object and return a keyword representing that object:
```clojure
nil -> :nil
true/false -> :boolean
\\c -> :char
1 -> :number
:hello -> :keyword
\"hello\" -> :string
hello -> :symbol
{:a :b} -> :map
#{:a :b} -> :set
[:a :b] -> :vector
(1 2 3) -> :list
:else -> (type sexp)
```"))
(extend-protocol SexpType
; literals
nil (simple-type [_sexp] :nil)
Boolean (simple-type [_sexp] :boolean)
Character (simple-type [_sexp] :char)
Number (simple-type [_sexp] :number)
String (simple-type [_sexp] :string)
clojure.lang.Keyword (simple-type [_sexp] :keyword)
clojure.lang.Symbol (simple-type [_sexp] :symbol)
; reader macros
clojure.lang.IPersistentMap (simple-type [_sexp] :map)
clojure.lang.IPersistentSet (simple-type [_sexp] :set)
clojure.lang.IPersistentVector (simple-type [_sexp] :vector)
clojure.lang.ISeq (simple-type [_sexp] :list)
; else
Object (simple-type [sexp] (symbol (pr-str (type sexp)))))
(comment
(simple-type {:a 1})
(simple-type (Object.))
(simple-type `(1 2 3)))