Skip to content

Instantly share code, notes, and snippets.

@maxweber
Created August 19, 2011 12:19
Show Gist options
  • Save maxweber/1156682 to your computer and use it in GitHub Desktop.
Save maxweber/1156682 to your computer and use it in GitHub Desktop.
pathWithComponents
(ns path.core
(:use clojure.test)
(:require [clojure.string :as s]))
(defn clean-component [component]
(s/replace component #"/+" ""))
(defn path-prefix [components]
(if (re-find #"/+" (first components))
"/"
""))
(defn pathWithComponents [components]
(apply str (path-prefix components)
(interpose "/"
(remove empty?
(map clean-component components)))))
(testing "pathWithComponents"
(is (= "Apple/Local/Library/Frameworks" (pathWithComponents ["Apple" "Local" "Library" "Frameworks"])))
(is (= "/Apple/Local/Library/Frameworks" (pathWithComponents ["/" "Apple" "Local" "Library" "Frameworks"])))
(is (= "/Apple/Local/Library/Frameworks" (pathWithComponents ["/" "Apple" "Local" "Library" "Frameworks/" "/"])))
(is (= "/Apple/Local/Library/Frameworks" (pathWithComponents ["/" "Apple" "Local" "///////" "Library" "Frameworks" "/"]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment