DEV Community

Discussion on: Credit card number check

Collapse
 
marconicolodi profile image
Marco Nicolodi • Edited

Ported to clojure

(defn valid-cc? [cc]
  (-> cc
      str
      (clojure.string/split #"")
      reverse
      (->> (map (fn [s] (Integer/parseInt s))))
      (->> (map-indexed (fn [i n] 
                          (if (not= 0 (mod i 2))
                              (if (>= (* 2 n) 10)
                                  (- (* 2 n) 9)
                                  (* 2 n))
                              n)))
           (reduce +))
      (mod 10)
      (= 0)))