/
core.cljc
134 lines (114 loc) · 5.17 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
(ns allpa.core
#?(:clj (:require [clojure.string :as string]
[clojure.walk :as walk]
[clojure.core.match]
[net.cgrand.macrovich :as macros])
:cljs (:require [cljs.core.match]))
#?(:cljs (:require-macros [allpa.core :refer [varg# deftagged match]]
[net.cgrand.macrovich :as macros]
[cljs.core.match]
)))
(def clj? #?(:clj true :cljs false))
;; util
(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)))))
;; 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))
;; i hate defrecord/defprotocol
(def mk (varg# (assoc %2 ::type %1)))
(def Default ::Default)
(def type ::type)
(def id ::id)
(def set-id #(assoc %1 ::id %2))
#?(:clj
(defmacro deftagged [label argv-raw]
(let [required (remove vector? argv-raw)
defaults (->> argv-raw
(filter vector?)
(mapcat #(-> %1))
(map #(-> [%1 %2]) (range))
(filter (fn [[id _]] (odd? id)))
(map #(nth %1 1)))
argv (->> argv-raw
(filter vector?)
(mapcat #(-> %1))
(map #(-> [%1 %2]) (range))
(filter (fn [[id _]] (even? id)))
(map #(nth %1 1))
(concat required))
splat (gensym "splat")]
`(defn ~label [& ~splat]
(when (< (count ~splat) ~(count required))
(throw (Err ~(str *ns* "/" label " must have at least " (count required) " arguments"))))
(when (> (count ~splat) ~(count argv))
(throw (Err ~(str *ns* "/" label " can have no more than " (count argv) " arguments"))))
(mk ~(keyword (str *ns*) (name label))
~(if (empty? argv) '{}
`(let [[~@argv] (concat ~splat (drop (- (count ~splat) ~(count required))
[~@defaults]))]
(hash-map ~@(mapcat #(-> [(keyword %1) %1]) argv)))))))))
#?(:clj
(defmacro match [v & specs]
`(~(macros/case :clj 'clojure.core.match/match
:cljs 'cljs.core.match/match)
~v
~@(->> specs
(map (fn [id form]
(if (odd? id) form
(walk/postwalk
(fn [form]
(if (not (list? form)) form
(let [[x & xs] form
tagged? (and (symbol? x)
(Character/isUpperCase (first (name x))))]
(if (not tagged?) form
(let [ns (or (namespace x)
(str (ns-name *ns*)))
nm (name x)]
(apply hash-map (-> ::type) (keyword ns nm)
(->> xs
(reduce
(fn [{:keys [kw? forms]} curr]
(cond
(not kw?) {:kw? true :forms (conj forms curr)}
(keyword? curr) {:kw? false :forms (conj forms curr)}
:else {:kw? true :forms (-> forms
(conj (keyword curr))
(conj curr))}))
{:kw? true :forms []})
:forms)))))))
form)))
(range))))))