Created
July 23, 2013 14:44
-
-
Save konr/6062892 to your computer and use it in GitHub Desktop.
CPF validator in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns backend.utils | |
(:require | |
[swiss-arrows.core :refer :all])) | |
(defn check-cpf [cpf] | |
(let [digits (->> cpf (re-seq #"\d") (map #(Integer. %))) | |
blacklisted? (partial re-find #"^12345678909|(\d)\1{10}$") | |
formatted? (partial re-find #"^\d{11}$") | |
checksum-ok? (fn [digit digits] | |
(-<> (for [i (range digit)] (* (nth digits i) (+ digit 1 (- i)))) | |
(apply + <>) (mod 11) (#(if (< % 2) 0 (- 11 %))) | |
(= (nth digits digit))))] | |
(and | |
(not (blacklisted? cpf)) | |
(formatted? cpf) | |
(checksum-ok? 9 digits) | |
(checksum-ok? 10 digits)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment