Created
September 6, 2014 17:25
-
-
Save pbalduino/9b8b738e505022aedc87 to your computer and use it in GitHub Desktop.
Retaining the head
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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