-
Notifications
You must be signed in to change notification settings - Fork 0
/
clojure.janet
48 lines (39 loc) · 970 Bytes
/
clojure.janet
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
# Clojure like shims.
(defn str [& r] (string/join r))
(defn conj
"Clojure like conj (sorta)."
[& r]
(table/to-struct (apply merge r)))
(defn concat
"Clojure like concat."
[& r]
(->> (reduce array/concat @[] r)
(apply tuple)))
(defn rest
"Clojure like rest."
[col]
(array/slice col 1))
(defn get-in
"Get a nested property path from a map."
[m ks & def]
(try
(reduce get m ks)
((error e) (or (first def) nil))))
(get-in {} [:a :b] 9)
(get-in {:a {:b 3} } [:a :b] 10)
(defn get-some
"Like get-in, but just go as far as possible in the path."
[m ks]
(let [maybe (get-in m ks)]
(if maybe
maybe
(get-in m (reverse (rest (reverse ks)))))))
(defn comp
"The built-in comp only accepts unary args for f(g(x)), we want g to be any arity.
Returns f(g(x1,...))."
[& fs]
(fn [& r]
(let [fns (array ;fs)
g (array/pop fns)
init (apply g r)]
(reduce (fn [h f] (f h)) init fns))))