/
core.cljc
204 lines (176 loc) · 7.4 KB
/
core.cljc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(ns allpa.core
(:require [allpa.util :as util]
[allpa.linked-hash-map :as lhm]
[clojure.walk :as walk])
#?(:clj (:require [clojure.string :as string]
[net.cgrand.macrovich :as macros]))
#?(:cljs (:require-macros [allpa.core :refer [varg# defprotomethod deftagged]]
[net.cgrand.macrovich :as macros])))
;; util
(def map-values util/map-values)
(def map-keys util/map-keys)
(def id util/id)
(def set-id util/set-id)
(defprotocol Simplify
(simple [this] "return a simpler version of the object. usually for printing"))
(extend-protocol Simplify
#?(:clj clojure.lang.PersistentArrayMap :cljs cljs.core.PersistentArrayMap)
(simple [m]
(if (::lhm/lhm? m) (lhm/to-vector m) m)))
(defn simplify [obj]
(walk/postwalk #(if (satisfies? Simplify %) (simple %) %) obj))
(defn curry [f & args2]
(fn [& args1]
(apply f (concat args1 args2))))
(def queue
#?(:clj (clojure.lang.PersistentQueue/EMPTY)
:cljs #queue []))
(defn filter-queue
([p q] (filter-queue p q queue))
([p q c]
(if (empty? q)
c
(let [v (peek q)
next (pop q)]
(filter-queue p next (if (p v) (conj c v) c))))))
(defn memoize
([f] (memoize f 10))
([f size]
(let [has (atom {})
saved (atom {})
lru (atom queue)]
(fn [& args]
(let [h (hash args)
has? (get @has h)]
(cond
has?
(do (swap! lru #(-> (filter-queue (curry not= h) %)
(conj h)))
(get @saved h))
(= size (count @saved))
(let [res (apply f args)
delete (peek @lru)]
(swap! has #(-> % (dissoc delete) (assoc h true)))
(swap! saved #(-> % (dissoc delete) (assoc h res)))
(swap! lru #(-> % pop (conj h)))
res)
:else
(let [res (apply f args)]
(swap! has #(assoc % h true))
(swap! saved #(assoc % h res))
(swap! lru #(conj % h))
res)))))))
(defn flip [f]
(fn [& args]
(apply f (last args) (->> args reverse (drop 1) reverse))))
(defn parse-int
([s] (parse-int s nil))
([s default]
(let [tried (try
#?(:cljs (js/parseInt s 10)
:clj (Integer/parseInt s))
#?(:cljs (catch js/Object e default)
:clj (catch Exception e default)))]
(if (int? tried) tried default))))
(defn num-lookup [n]
(nth ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
"N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"]
n))
(defn Err [arg]
#?(:clj (Exception. arg)
:cljs (js/Error. arg)))
(defn alpha-num [n]
(if (< n 26)
(num-lookup n)
(str (alpha-num (dec (quot n 26))) (num-lookup (mod n 26)))))
(defn snake [buckets a]
(let [size (count a)
safe-nth #(if (>= %1 size) nil (nth a %1))
hm (reduce (fn [hm i]
(reduce (fn [hm j]
(update hm i #(-> %1
(conj (safe-nth (+ (* 2 buckets j) i)))
(conj (safe-nth (+ (* 2 buckets j)
(- (* 2 buckets) i 1)))))))
hm
(range (inc (quot (quot size buckets) 2)))))
{}
(range buckets))]
(->> (range buckets)
(map #(->> (get hm %1) (remove nil?) reverse vec))
vec)))
(defn unsnake [aa]
(let [nnth (fn [i j]
(let [a (if (>= i (count aa)) nil (nth aa i))]
(if (>= j (count a)) nil (nth a j))))
outer (count aa)
inner (reduce #(max %1 (count %2)) 0 aa)]
(->> (range (inc (quot inner 2)))
(mapcat (fn [j]
(concat (->> (range (inc outer))
(map (fn [i] (nnth i (* 2 j))))
(remove nil?))
(->> (range outer -1 -1)
(map (fn [i] (nnth i (inc (* 2 j)))))
(remove nil?)))))
vec)))
;; macros
#?(:clj
(defmacro varg# [& statements]
(let [args (gensym "args")]
`(fn [& ~args]
~@(->> statements
(walk/postwalk (fn [sym]
(let [sym-name (if (symbol? sym) (name sym) "")]
(if (string/starts-with? sym-name "%")
`(nth ~args ~(-> sym-name (subs 1) (parse-int -1) dec) nil)
sym)))))))))
;; math
(defn power [b e]
(if (< e 0) 0
#?(:clj (int (Math/floor (Math/pow b e)))
:cljs (.round js/Math (.floor js/Math (.pow js/Math b e))))))
(def p2 (partial power 2))
;; other
(defrecord Ok [result])
(defrecord Fail [error])
(defn ensure-vec [mv] (if (vector? mv) mv [mv]))
(defprotocol Tagged
(tag [_] "keyword representing record's type"))
#?(:clj
(defmacro deftagged [sym args]
`(defrecord ~sym ~args
Tagged
(tag [_#] ~(keyword (str *ns*) (name sym))))))
#?(:clj
(defmacro defprotomethod [method args & defs]
`(do (defprotocol ~(symbol (str "proto-" (name method)))
~(clojure.core/list method (vec (map (fn [_] (gensym "arg")) args))))
(extend-protocol ~(symbol (str "proto-" (name method)))
~@(mapcat (fn [[types body]]
(mapcat (fn [type]
(let [stype (str type)
type
(if (string/starts-with? stype "!")
(let [tname (name type)
tname (cond
(string/starts-with? tname "->") (subs tname 2)
(string/starts-with? tname "!->") (subs tname 3)
(string/starts-with? tname "!") (subs tname 1)
:else tname)
cons (symbol (when (namespace type)
(subs (namespace type) 1))
(str "->" tname))]
(macros/case :clj (symbol (str
(-> cons resolve meta :ns str
(string/replace #"-" "_"))
"."
tname))
:cljs (symbol (or (namespace cons)
(str (get (-> &env :ns :uses)
(symbol (name cons)))))
(subs (name cons) 2))))
type)]
[type `(~method ~args ~body)]))
(ensure-vec types)))
(partition 2 defs))))))