Skip to content

Instantly share code, notes, and snippets.

@konr
Created July 23, 2013 14:44
Show Gist options
  • Save konr/6062892 to your computer and use it in GitHub Desktop.
Save konr/6062892 to your computer and use it in GitHub Desktop.
CPF validator in Clojure
(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