-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.cljc
180 lines (159 loc) · 6.04 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
(ns peanuts.core
(:require
[clojure.walk :as walk]
[clojure.set :as cljset]))
(defn- not-empty-coll [thing]
(if (coll? thing) (not-empty thing) thing))
(defn- not-empty-vals [thing]
(cond
(map? thing) (some #(not-empty-coll (second %)) thing)
(coll? thing) (not-empty-coll thing)
thing thing
:else nil))
(defn- binding-vector? [thing]
(and (vector? thing) (-> thing first symbol?)))
(defn- remove-deep [key-set data]
(if (not-empty key-set)
(->> data
(walk/prewalk (fn [node]
(cond
(binding-vector? node) (vec (remove key-set node))
(map? node) (apply dissoc (dissoc node :or) key-set)
(get key-set node) nil
:else node)))
(filterv not-empty-vals))
data))
(defn- symbol-in? [key-set s]
(if (symbol? s)
(get key-set s)
s))
(defn- filter-symbol-keys [key-set m]
(reduce-kv (fn [c k v]
(if (symbol-in? key-set k)
(assoc c k v)
c))
{}
m))
(defn- filter-deep [key-set data]
(if (not-empty key-set)
(->> data
(walk/prewalk (fn [node]
(cond
(binding-vector? node) (filterv #(symbol-in? key-set %) node)
(map? node) (filter-symbol-keys key-set (dissoc node :or))
(not (symbol-in? key-set node)) nil
:else node)))
(filterv not-empty-vals)
(remove #{'&}))
data))
(defn get-associative-destructuring-vector [node]
(and (or (map? node) nil)
(select-keys node #{:keys :strs :syms})))
(defn- flatten-maps [args]
(->> args
(walk/prewalk (fn [node]
(let [assoc-dest-vec (get-associative-destructuring-vector node)]
(cond
(not-empty assoc-dest-vec) (vec (vals assoc-dest-vec))
(map? node) (vec (keys node))
:else node))))
flatten
distinct))
(defn- subscribe-form [binding-vec default]
`(~'if-let [~'sub (~'re-frame.core/subscribe ~binding-vec)]
~'(deref sub)
~default))
(def ^:private redlist-options [:exempt :redlist :rl :redlist1 :rl1])
(defn rm-rl1-meta-form [binding]
`(~'vary-meta ~binding ~'dissoc :rl1 :redlist1))
(defn- cond-form [binding binding-args]
(let [binding-vec (into [binding] binding-args)]
`(~'cond
(~'let [~'rl-opts (~'select-keys (~'meta ~binding) ~redlist-options)] ~'(some identity rl-opts)) ~(rm-rl1-meta-form binding)
(~'keyword? ~binding) ~(subscribe-form binding-vec binding)
(~'and (~'vector? ~binding) (~'keyword? (~'first ~binding))) ~(subscribe-form binding binding)
(~'or (~'-> ~binding ~'meta :sub-fn) (~'and (~'fn? ~binding) ~binding-args)) ~(seq binding-vec)
:else ~binding)))
(defn- seq->let-form [args seq*]
(->> seq*
(reduce (fn [c b]
(-> c
(conj b)
(conj (cond-form b (get args b)))))
[])
(conj '(let))
reverse))
(defn- quote-symbols [coll]
(walk/postwalk (fn [v] (if (symbol? v) (list 'quote v) v)) coll))
(defn- redlist-meta? [binding]
(let [{:keys [exempt redlist]} (meta binding)]
(or exempt redlist)))
(defn- peanut
[n f {:keys [exempt greenlist redlist only def? sub-args] :as meta-map}]
(let [[_ args & body] f
bindings (->> args
(remove-deep (cljset/union (set exempt) (set redlist)))
vec
(filter-deep (cljset/union (set only) (set greenlist)))
flatten-maps
(remove (some-fn #{'&} redlist-meta?))
(seq->let-form sub-args))
symbol-quoted-meta-map (quote-symbols meta-map)]
(cond->> body
'->> (concat bindings)
'->> list
'->> (concat `(~'fn ~args))
def? list
def? (concat `(def ~(->> n
meta
(merge symbol-quoted-meta-map)
(with-meta n)))))))
(defmacro fc
([f opts]
(peanut nil f (merge opts {:def? false})))
([f] `(fc ~f {})))
(defmacro defc
([n f opts]
(peanut n f (merge opts {:def? true})))
([n f]
`(defc ~n ~f {})))
(defn- merge-meta-maps [meta-map1 [meta-map2 :as _fnbody] & other-maps]
(cond-> (apply merge meta-map1 other-maps)
(map? meta-map2) (merge meta-map2)))
(defmacro defnc
"Takes similar arguments to defn and returns a similar result.
The returned function body will be wrapped in a let-block which will
conditionally rebind the function args to values of re-frame subscriptions."
[n & [doc-str meta-map & [args & body :as args&body]]]
(cond
;; no doc-str or metadata map
(vector? doc-str) (let [args* doc-str
body* (into [meta-map] args&body)]
`(defnc ~n nil {} ~args* ~@body*))
;; metadata map, but no doc-str
(map? doc-str) (let [meta-map* doc-str
args* meta-map
body* args&body]
`(defnc ~n nil ~meta-map* ~args* ~@body*))
;; doc-str, but no metadata map
(vector? meta-map) (let [args* meta-map
body* args&body]
`(defnc ~n ~doc-str {} ~args* ~@body*))
:else (peanut
n
`(fn ~args ~@body)
(merge-meta-maps
meta-map
body
{:def? true}
(when doc-str {:doc doc-str})))))
(defmacro fnc
"Returns an fn form.
The returned function body will be wrapped in a let-block which will
conditionally rebind the function args to values of re-frame subscriptions."
[& [opts args & body]]
(if (vector? opts)
(let [body* (into [args] body)
args* opts]
`(fnc {} ~args* ~@body*))
(peanut nil `(fn ~args ~@body) (merge-meta-maps opts body {:def? false}))))