Skip to content

Instantly share code, notes, and snippets.

@humbertodosreis
Created February 18, 2020 10:51
Show Gist options
  • Save humbertodosreis/7cd4467f369f322fde5cdf286ee6e1b2 to your computer and use it in GitHub Desktop.
Save humbertodosreis/7cd4467f369f322fde5cdf286ee6e1b2 to your computer and use it in GitHub Desktop.
(ns playground.mutimethods)
(defrecord DeclaracaoSimplificada [nome cpf total-redimentos])
(defrecord DeclaracaoCompleta [nome cpf total-redimentos despesas])
(defrecord Despesa [descricao valor])
(defn imposto-a-pagar-b [declaracao]
(if (record? DeclaracaoSimplificada)
(* 0.15 (:total-redimentos declaracao))
(* 0.275 (:total-redimentos declaracao))))
(print (imposto-a-pagar-b (->DeclaracaoSimplificada "Pedro" "123132" 5000)))
(print (imposto-a-pagar-b
(->DeclaracaoCompleta "Pedro" "123132" 15000 [
(->Despesa "medico" 50)])))
;
; definimos um dispatch pelo tipo da classe
(defmulti imposto-a-pagar (fn [declaracao] (class declaracao)))
; implementação
(defmethod imposto-a-pagar DeclaracaoSimplificada [declaracao]
(* 0.15 (:total-redimentos declaracao)))
(defmethod imposto-a-pagar DeclaracaoCompleta [declaracao]
(* 0.275 (:total-redimentos declaracao)))
(print (imposto-a-pagar (->DeclaracaoSimplificada "Humberto" "31291157816" 248000)))
(print (imposto-a-pagar (->DeclaracaoCompleta "Humberto" "31291157816" 1000 [])))
; protocols
(defprotocol CalculoDeImpostoDeRenda
(imposto-a-pagar [this]))
(extend-protocol CalculoDeImpostoDeRenda
DeclaracaoCompleta
(imposto-a-pagar [this] (* 0.12 (:total-redimentos this))))
(extend-protocol CalculoDeImpostoDeRenda
DeclaracaoSimplificada
(imposto-a-pagar [this] (* 0.27 (:total-redimentos this))))
(print (imposto-a-pagar (->DeclaracaoCompleta "Jose" "123" 1000 [])))
(print (imposto-a-pagar (->DeclaracaoSimplificada "Maria" "123" 50000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment