Skip to content

Instantly share code, notes, and snippets.

@saivenkat
Created January 29, 2010 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saivenkat/289621 to your computer and use it in GitHub Desktop.
Save saivenkat/289621 to your computer and use it in GitHub Desktop.
The problem: given a list of strings, produce a list where sequential non-empty strings are concatenate
(use 'clojure.test)
(defn concat-elements [x y]
(cond (empty? x) (apply str (concat "-" y))
(empty? y) (apply str (concat x "-"))
:else (apply str(concat x y))))
(defn glom [x]
(remove #(empty? %)
(seq (.split (apply str (reduce concat-elements x)) "-"))))
(deftest join-list-members-when-no-empty-between
(is (= '("ab") (glom '("a" "b")))))
(deftest donot-join-list-members-when-empty-is-between
(is (= '("a" "b") (glom '("a" "" "b"))))
(is (= '("a" "bc") (glom '("a" "" "b" "c"))))
(is (= '("a" "bc") (glom '("a" "" "" "b" "c"))))
(is (= '("a" "b" "c") (glom '("a" "" "b" "" "c")))))
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment