Skip to content

Instantly share code, notes, and snippets.

@knjname
Last active August 7, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knjname/f1f7ac337052a9439db5e00a1c3d47e8 to your computer and use it in GitHub Desktop.
Save knjname/f1f7ac337052a9439db5e00a1c3d47e8 to your computer and use it in GitHub Desktop.
(defn
validate-my-number [mynumber]
(and (re-matches #"^\d{12}$" mynumber)
(let [digits (map int mynumber)
[xs [check_digit]] (split-at 11 digits)
each_digit_fn (fn [num idx] (* num (if (<= idx 6) (+ idx 1) (- idx 5))))
calculated_digit (as-> xs <>
(reverse <>)
(map each_digit_fn <> (iterate inc 1))
(reduce + <>)
(mod <> 11)
(if (<= <> 1) 0 (- 11 <>)))]
(= check_digit calculated_digit))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment