Fork me on GitHub
#code-reviews
<
2022-02-18
>
Lukas Domagala16:02:12

So I just wrote this monster and am wondering if there’s a smarter / cleaner way to do these kinds of things:

(defn- process-paragraph [section-string]
  (->> (some-> (re-find #"\*((.|\n)*?)\*" section-string)
               second
               (str/split #", "))
       (map #(str/replace % #"\(((.|\n)*?)\)" ",$1"))
       (mapcat #(let [[x & xs] (str/split % #",")]
                  (if xs
                    (map (fn [num] (str x num)) xs)
                    [x])))))

(process-paragraph "ignore, *3.5(a)(b)(c)(d)(e), 3.6*")
;; => ("3.5a" "3.5b" "3.5c" "3.5d" "3.5e" "3.6")

emccue17:02:30

I would say there are two parts 1. maybe use a "verbose regex" library 2. Split out more steps into separate named functions

upvote 2