/
t2.fnl
31 lines (27 loc) · 867 Bytes
/
t2.fnl
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
; Task 2 — Common Paths
(local DEFAULT-INPUT
["/a/b/c/1/x.pl"
"/a/b/c/d/e/2/x.pl"
"/a/b/c/d/3/x.pl"
"/a/b/c/4/x.pl"
"/a/b/c/d/5/x.pl"])
(fn common-path
[coll]
(let [ncoll (length coll)
parts (icollect [_ path (ipairs coll)]
(icollect [part (-> path (: :gmatch "(/*[^/]*/*)"))]
part))
result (icollect [i v (ipairs (. parts 1))]
(when (= ncoll (accumulate [sum 0 _ p (ipairs parts)]
(+ sum (if (= v (. p i)) 1 0))))
v))]
(table.concat result "")))
(fn main
[]
(match arg
(where ["-e" _ & n] (< 0 (length n))) (print (common-path n))
["-e" _ & n] (print (common-path DEFAULT-INPUT))
_ (do
(print "error: see README.md for invocation")
(os.exit 1))))
{: main : common-path : DEFAULT-INPUT}