Created
January 15, 2014 07:09
-
-
Save ghoseb/8432086 to your computer and use it in GitHub Desktop.
Uniquify buffer names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ^{:doc "Uniquify" | |
:author "Baishampayan Ghose <[email protected]>"} | |
uniquify | |
(:require [clojure.string :refer [join]])) | |
(defn explode | |
"Explode a directory name to its subcomponents." | |
[^String dir] | |
(seq (.split dir "/"))) | |
(defn tails | |
"Successive tails of a collection." | |
[coll] | |
(when-not (empty? coll) | |
(cons coll (tails (rest coll))))) | |
(defn paths | |
"Given an exploded sequence, get all possible sub-paths." | |
[xs] | |
(map (partial join "/") xs)) | |
(defn index | |
"Index all possible subcomponents with their counts." | |
[path-coll] | |
(reduce (fn [res p] (merge-with + res (frequencies p))) {} path-coll)) | |
(defn uniquify | |
"Uniquify the string representations of all paths." | |
[ps] | |
(let [ps (map (comp paths tails explode) ps) | |
idx (index ps)] | |
(map first | |
(for [p (map reverse ps)] | |
(for [x p :when (= (idx x) 1)] | |
x))))) | |
;;; (def in ["a/b/c" "a/c/d" "a/b/d" "c/b/d" "a/c/x"]) | |
;;; (def out ["c" "c/d" "a/b/d" "c/b/d" "x"]) | |
;;; (uniquify in) | |
;;; ;=> ("c" "c/d" "a/b/d" "c/b/d" "x") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment