/
string.clj
135 lines (115 loc) · 3.07 KB
/
string.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
(ns contextual.impl.string
(:require
[contextual.impl.protocols :as p]))
(set! *warn-on-reflection* true)
(defrecord Str [args]
p/IContext
(-invoke [this ctx]
(let [^StringBuilder sb (StringBuilder.)]
(p/-invoke-with-builder this ctx sb)
(.toString sb)))
p/IStringBuild
(-invoke-with-builder [this ctx sb]
(doseq [arg args]
(p/-invoke-with-builder arg ctx sb))
(.toString ^StringBuilder sb)))
(defn ->str!
[args]
(->Str args))
(defn ->join!
[delim args]
(->Str (interpose delim args)))
(defonce ^:private str-builders (atom {}))
(defmacro ^:private def-str []
(let [ctx 'ctx
name "Str"
sb (with-meta 'sb {:tag "StringBuilder"})
defs
(for [n (range 1 23)
:let [args (map (comp symbol #(str "a" %)) (range n))
rec (symbol (str name n))
constructor (symbol (str "->" rec))
parts (map (fn [a] `(p/-invoke-with-builder ~a ~ctx ~sb)) args)]]
`(do
(defrecord ~rec [~@args]
p/IStringBuild
(-invoke-with-builder [~'this ~ctx ~'sb]
~@parts)
p/IContext
(-invoke [~'this ~ctx]
(let [~sb (StringBuilder.)]
(p/-invoke-with-builder ~'this ~ctx ~sb)
(.toString ~sb))))
(swap! str-builders assoc ~n ~constructor)))]
`(do
~@defs)))
(def-str)
(defn ->str
[& args]
(let [n (count args)
c (get @str-builders n)]
(if c
(apply c args)
(->Str args))))
(comment
(p/-invoke (->str 1 2 3) nil))
(defn ->join
[delim & args]
(let [args (interpose delim args)]
(apply ->str args)))
(def compress-string-xf
(comp
(remove nil?)
(partition-by (some-fn string? char?))
(mapcat
(fn [xs]
(if ((some-fn string? char?) (first xs))
[(apply str xs)]
xs)))))
(comment
(transduce compress-string-xf conj [] '[a b "c" d "e" "f" g])
(transduce compress-string-xf conj [] '["0" a b "c" d "e" "f" g])
(transduce compress-string-xf conj [] '["0" a b "c" d "e" \= "f" g])
(transduce compress-string-xf conj [] '["0" a b "c" (path :x y) "e" \= "f" g]))
(defn strexpr?
[expr]
(= (first expr) 'str))
(defn unnest-str1
[expr]
(assert (= 'str (first expr)) "must only be called on str expression.")
(mapcat
(fn [expr]
(if (and (seq? expr) (strexpr? expr))
(rest expr)
[expr]))
expr))
(defn unnest-str
[expr]
(let [expr' (unnest-str1 expr)]
(if (= expr expr')
expr
(recur expr'))))
(defn unnest-str1*
[expr]
(assert (strexpr? expr) "must only be called on str expression.")
(apply
list
(transduce
(comp
(mapcat
(fn [expr]
(if (and (seq? expr) (strexpr? expr))
(rest expr)
[expr])))
compress-string-xf)
conj
expr)))
(deftype Sbf [f]
p/IStringBuild
(-invoke-with-builder [this ctx sb]
(f sb)))
(defn ->sbf [f] (Sbf. f))
(defn trim-sb
[^StringBuilder sb]
(.setLength sb (unchecked-dec-int (.length sb))))
(def trim (->sbf trim-sb))