Find common directory path: Difference between revisions

Added Clojure.
(Oops, corrected alphabetic ordering)
(Added Clojure.)
Line 9:
Note: The resultant path should be the valid directory <code>'/home/user1/tmp'</code> and not the longest common string <code>'/home/user1/tmp/cove'</code>.<br>
If your language has a routine that performs this function (even if it does not have a changeable separator character, then mention it as part of the task)
 
=={{header|Clojure}}==
<lang clojure>(defn common-prefix [sep paths]
(let [parts-per-path (map #(.split (re-pattern sep) %) paths)
common-parts (for [part-list (apply map vector parts-per-path)
:when (apply = part-list)] (first part-list))]
(apply str (interpose sep common-parts))))
 
(println
(common-prefix "/"
["/home/user1/tmp/coverage/test"
"/home/user1/tmp/covert/operator"
"/home/user1/tmp/coven/members"]))</lang>
 
=={{header|J}}==
Anonymous user