Skip to content

Instantly share code, notes, and snippets.

@pbalduino
Created September 6, 2014 17:25
Show Gist options
  • Select an option

  • Save pbalduino/9b8b738e505022aedc87 to your computer and use it in GitHub Desktop.

Select an option

Save pbalduino/9b8b738e505022aedc87 to your computer and use it in GitHub Desktop.
Retaining the head
(defn better-count [lista] ; 1
(loop [lista lista ; 2
items 0] ; 3
(if (empty? lista) ; 4
items ; 5
(recur (rest lista) ; 6
(inc items))))) ; 7
;; ----------------------------
;; o jeito errado:
(def lista-inteira (->> (range 1 1e10)
(map #(* % 33))
(filter #(= 0 (mod % 17)))))
(better-count lista-inteira)
; OutOfMemoryError GC overhead limit exceeded
;; ----------------------------
;; o jeito certo
(def contagem (->> (range 1 1e10)
(map #(* % 33))
(filter #(= 0 (mod % 17)))
better-count))
contagem
; 588235294
;; ----------------------------
;; o jeito melhor
(def contagem (->> (range 1 1e10)
(map #(* % 33))
(filter #(= 0 (mod % 17)))
(reduce (fn [contagem _] (inc contagem)) 0)))
contagem
; 588235294
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment