Sha256: 91abdf98f444360c1669cf2063e1f4f25aaa601ab664957619e3341616b3b681
Contents?: true
Size: 780 Bytes
Versions: 107
Compression:
Stored size: 780 Bytes
Contents
(ns luhn) (defn to-reversed-digits "returns a lazy sequence of least to most significant digits of n" [n] (->> [n 0] (iterate (fn [[i _]] [(quot i 10) (mod i 10)])) (take-while (complement #{[0 0]})) (map second) rest)) (defn checksum "returns the luhn checksum of n, assuming it has a check digit" [n] (-> (->> n to-reversed-digits (map * (cycle [1 2])) (map #(if (>= % 10) (- % 9) %)) (apply +)) (mod 10))) (defn valid? "whether n has a valid luhn check-digit" [n] (zero? (checksum n))) (defn add-check-digit "given a number, adds a luhn check digit at the end" [n] (let [n-shifted (* 10 n) check-digit (- 10 (checksum n-shifted))] (+ n-shifted check-digit)))
Version data entries
107 entries across 107 versions & 1 rubygems