-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.cljs
55 lines (50 loc) · 1.33 KB
/
util.cljs
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
(ns keechma.next.util)
(defn get-dirty-deps [prev-deps next-deps]
(let [dirty
(reduce
(fn [m k]
(let [v (get next-deps k)]
(if-not (identical? v (get prev-deps k))
(assoc m k v)
m)))
{}
(set (concat (keys prev-deps) (keys next-deps))))]
(if (empty? dirty)
nil
dirty)))
(defn lexicographic-compare
([xs ys]
(lexicographic-compare compare xs ys))
([compare xs ys]
(loop [xs (seq xs) ys (seq ys)]
(if xs
(if ys
(let [c (compare (first xs) (first ys))]
(if (not (zero? c))
c
(recur (next xs), (next ys))))
1)
(if ys
-1
0)))))
(defn sort-paths [paths]
(sort lexicographic-compare paths))
(defn find-common-subvec [v1 v2]
(let [max-idx (min (count v1) (count v2))]
(if (pos? max-idx)
(loop [idx 0]
(if (and (= (get v1 idx) (get v2 idx))
(< idx max-idx))
(recur (inc idx))
(subvec v1 0 idx)))
[])))
(defn get-lowest-common-ancestor-for-paths [paths]
(let [[f & r] (reverse (sort paths))]
(reduce
(fn [acc v]
(let [common-subvec (find-common-subvec acc v)]
(if (= [] common-subvec)
(reduced [])
common-subvec)))
f
r)))