Created
January 29, 2010 10:18
-
-
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
This file contains 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
(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