From 2c3463a677d01dfc027bc518de91eeeaebc14a5b Mon Sep 17 00:00:00 2001 From: Andinus Date: Mon, 6 Sep 2021 18:33:47 +0530 Subject: Clojure: Raindrops: Improve solution Avoids logic repetition. --- clojure/raindrops/src/raindrops.clj | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'clojure') diff --git a/clojure/raindrops/src/raindrops.clj b/clojure/raindrops/src/raindrops.clj index 8815f2d..fda3fef 100644 --- a/clojure/raindrops/src/raindrops.clj +++ b/clojure/raindrops/src/raindrops.clj @@ -1,15 +1,11 @@ (ns raindrops) (defn convert [number] - (def drops "") - - (if (== (mod number 3) 0) - (def drops (str drops "Pling"))) - (if (== (mod number 5) 0) - (def drops (str drops "Plang"))) - (if (== (mod number 7) 0) - (def drops (str drops "Plong"))) - - (if (= drops "") - (def drops (str number))) - drops) + (let [drops (apply str + (map (fn [factor string] + (if (== (mod number factor) 0) + string nil)) + [3 5 7] + ["Pling" "Plang" "Plong"]))] + (if (= drops "") + (str number) drops))) -- cgit 1.4.1-2-gfad0