Skip to content

Instantly share code, notes, and snippets.

@geraldodev
Last active January 22, 2020 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraldodev/0289d49038990deaa2e407156b6afc9b to your computer and use it in GitHub Desktop.
Save geraldodev/0289d49038990deaa2e407156b6afc9b to your computer and use it in GitHub Desktop.
into_com_transducer
(ns into-com-transducer
[criterium.core :refer [bench]])
; coloque no deps.dn
; criterium {:mvn/version "0.4.5"}
(def campo-exemplo
#:columninfo{:remarks "COMPET�NCIA FINAL",
:is_autoincrement "NO",
:column_size 6,
:is_nullable "NO",
:char_octet_length 6,
:jb_is_identity "NO",
:num_prec_radix 10,
:data_type 1,
:column_name "CH_H_CMPT_FIM",
:table_name "TB_CHECK_HOSP",
:nullable 0,
:type_name "CHAR",
:ordinal_position 4,
:is_generatedcolumn "NO"
:teste_nil nil
}
)
;; utiliza forma (into to transducer from)
;; pelo bench o tempo médio foi a metade do abaixo que utiliza lazy sequences
;; como remove e map nao tem o terceiro parametro da collection eles retornam transducers
;; comp composicao normal resolve da direita pra esquerda, mas quando se usa transducer
;; a ordem passa a ser a natural. de forma que aqui primeiro vai ser executado o remove
;; depois vai ser executado o map
(bench
(into {}
(comp
(remove (fn [[k v]] (nil? v)))
(map (fn [[k v :as entry]]
(if (get #{:columninfo/is_generatedcolumn
:columninfo/is_autoincrement
:columninfo/is_nullable} k)
[k (get {"YES" true "NO" false "" nil} v)]
entry)))
)
campo-exemplo))
;; mesmo algoritmo utilizando lazy sequence
(bench
(->> campo-exemplo
(remove (fn [[k v]] (nil? v)))
(map (fn [[k v :as entry]]
(if (get #{:columninfo/is_generatedcolumn
:columninfo/is_autoincrement
:columninfo/is_nullable} k)
[k (get {"YES" true "NO" false "" nil} v)]
entry)))
(into {})))
;; nos dois é importante lembrar que ao percorrer um mapa obtem-se um map-entry, que é um vetor com dois itens
;; [chave valor] por isso a função desestrutura assim (fn [[k v]] ....)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment