about summary refs log tree commit diff
path: root/fnl/nvrc/utils.fnl
blob: 04398b4aef6109ace53d7ca756942d4533a2c7ce (plain)
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
(fn tbl? [x]
  (= :table (type x)))

(fn count [xs]
  (if (tbl? xs) (table.maxn xs)
      (not xs) 0
      (length xs)))

(fn run! [f xs]
  "Execute the function (for side effects) for every xs."
  (when xs
    (let [nxs (count xs)]
      (when (> nxs 0)
        (for [i 1 nxs]
          (f (. xs i)))))))

(fn reduce [f init xs]
  "Reduce xs into a result by passing each subsequent value into the fn with
  the previous value as the first arg. Starting with init."
  (var result init)
  (run! (fn [x]
          (set result (f result x))) xs)
  result)

(fn merge! [base ...]
  (reduce (fn [acc m]
            (when m
              (each [k v (pairs m)]
                (tset acc k v)))
            acc) (or base {}) [...]))

(fn merge [...]
  (merge! {} ...))

{: merge}