~willvaughn/kata

2c1d9ca1ea7ade3560598e97e5ef4eb105dfc598 — William Vaughn 1 year, 3 months ago b0891ed main
codewars strip-comments solution

not a particularly good solution though
A codewars/src/clj/org/willvaughn/codewars/strip_comments.clj => codewars/src/clj/org/willvaughn/codewars/strip_comments.clj +15 -0
@@ 0,0 1,15 @@
(ns org.willvaughn.codewars.strip-comments
  (:require [clojure.string :as s]))

(defn maybe-escape [x]
  (if-let [c (#{\! \$ \?} x)]
    (str \\ x)
    x))

(defn strip-comments [text comment-symbols]
  (let [safe-symbols (s/join (map maybe-escape comment-symbols))
        p (re-pattern (str "([" safe-symbols "]+.*)$"))]
    (->> (for [l (s/split text #"\n")]
           (s/replace l p ""))
         (map s/trimr)
         (s/join "\n"))))

A codewars/test/clj/org/willvaughn/codewars/strip_comments_test.clj => codewars/test/clj/org/willvaughn/codewars/strip_comments_test.clj +15 -0
@@ 0,0 1,15 @@
(ns org.willvaughn.codewars.strip-comments-test
  (:require [clojure.test :refer :all]
            [org.willvaughn.codewars.strip-comments :refer [strip-comments]]))
            
            
(deftest strip-comments-tests
  (are [text symbols expected] (= (strip-comments text symbols) expected)
    "apples, pears # and bananas\ngrapes\nbananas !apples"
    '("#" "!")
    "apples, pears\ngrapes\nbananas"
    
    "a #b\nc\nd $e f g"
    '("#" "$")
    "a\nc\nd"
))