Skip to content

Instantly share code, notes, and snippets.

@bragil
Last active March 15, 2016 16:43
Show Gist options
  • Save bragil/eca04395ba79336f4b09 to your computer and use it in GitHub Desktop.
Save bragil/eca04395ba79336f4b09 to your computer and use it in GitHub Desktop.
Cálculo do DV do CPF em Clojure
(ns bragil.dv-cpf)
; Realiza o cálculo do DV do CPF
(defn calc-dv [cpf-sem-dv]
"Calcula cada dígito do DV do CPF"
(def dv (mod
(*
(reduce +
(map-indexed
(fn [i c] (* (Integer/parseInt (str c)) (- (inc (count cpf-sem-dv)) i)))
(seq cpf-sem-dv)))
10)
11))
(if (> dv 9) 0 dv)
)
(defn get-dv-cpf [cpf-sem-dv]
"Retorna o DV do CPF passado"
(def sem-dv (subs cpf-sem-dv 0 9))
(def dv1 (calc-dv sem-dv))
(def dv2 (calc-dv (str sem-dv dv1)))
(str dv1 dv2))
(defn -main [& args]
(if (= (count args) 1)
(println (str (get-dv-cpf (first args))))
(println "Uso: lein run <CPF sem DV>")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment