Skip to content

Instantly share code, notes, and snippets.

@iammateus
Last active January 4, 2022 00:49
Show Gist options
  • Save iammateus/62c584c629b8d3f517756d20b89189aa to your computer and use it in GitHub Desktop.
Save iammateus/62c584c629b8d3f517756d20b89189aa to your computer and use it in GitHub Desktop.
CPF validation in Clojure
(ns tutorial.cpf
(:gen-class))
(defn validate_first_part
[cpf]
(loop [index 0 counter 0]
(def incre (* (- 10 index) (Integer/parseInt (.toString (.charAt cpf index)))))
(if (= index 9)
(do
(def lastDigit (Integer/parseInt (.toString (.charAt cpf 9))))
(def countMod (mod (* counter 10) 11))
; (println "validate_first_part" "lastDigit" lastDigit)
; (println "validate_first_part" "countMod" countMod)
(cond (= countMod 10) (if (= lastDigit 0) "True" "False")
(= countMod 11) (if (= lastDigit 0) "True" "False")
:else (if (= lastDigit countMod) "True" "False"))
)
(recur (+ index 1) (+ counter incre) ))))
(defn validate_second_part
[cpf]
(loop [index 0 counter 0]
(def incre (* (- 11 index) (Integer/parseInt (.toString (.charAt cpf index)))))
(if (= index 10)
(do
(def lastDigit (Integer/parseInt (.toString (.charAt cpf 10))))
(def countMod (mod (* counter 10) 11))
; (println "validate_second_part" "lastDigit" lastDigit)
; (println "validate_second_part" "countMod" countMod)
(cond (= countMod 10) (if (= lastDigit 0) "True" "False")
(= countMod 11) (if (= lastDigit 0) "True" "False")
:else (if (= lastDigit countMod) "True" "False"))
)
(recur (+ index 1) (+ counter incre) ))))
(defn validate
"Validates a cpf"
[ cpf ]
(if (= (count cpf) 11) (do
(def result_first_part (validate_first_part cpf))
; (println "result_first_part" result_first_part)
(if(= result_first_part "True")
(do
(def result_second_part (validate_second_part cpf))
; (println "result_second_part" result_second_part)
(if (= result_second_part "True") "True" "False")
) "False"))
"False"))
@iammateus
Copy link
Author

@iammateus
Copy link
Author

Pegar um valor pra testar em: https://www.4devs.com.br/gerador_de_cpf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment