Sha256: 4ffc90b0c0e96c8426e0b635da6335204195c705283e2efd5d956c9c064fdceb
Contents?: true
Size: 945 Bytes
Versions: 289
Compression:
Stored size: 945 Bytes
Contents
(ns luhn (:require [clojure.string :as string])) (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 string->long "Strips any non-digit characters and converts the string into a Long" [n] (-> n (string/replace #"[^0-9]+" "") Long/parseLong)) (defn valid? "whether n has a valid luhn check-digit" [n] ; Numbers with non digit/whitespace or only 1 digit are invalid (if (or (re-find #"[^0-9\s]+" n) (>= 1 (count (string/trim n)))) false (zero? (-> n string->long checksum))))
Version data entries
289 entries across 289 versions & 1 rubygems