Created
November 4, 2016 19:32
-
-
Save bfabry/cfed8fe2ab3b2f323c275d9b7efd699b to your computer and use it in GitHub Desktop.
This file contains 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
(reduce | |
(fn [acc v] | |
(let [v' (dec v)] | |
(if-not (and (even? v') (> v' 2)) | |
acc | |
(+ acc v')))) | |
0 | |
[1 2 3 4 5 6 7]) | |
=> 10 | |
(defn run-handwritten [] (reduce | |
(fn [acc v] | |
(let [v' (dec v)] | |
(if-not (and (even? v') (> v' 2)) | |
acc | |
(+ acc v')))) | |
0 | |
[1 2 3 4 5 6 7])) | |
=> #'user/run-handwritten | |
(def xform | |
(comp | |
(map dec) | |
(filter even?) | |
(filter #(> % 2)))) | |
(defn run-transducer [] (transduce xform + [1 2 3 4 5 6 7])) | |
=> #'user/xform | |
=> #'user/run-transducer | |
(defn run-lazyseqs [] (->> | |
[1 2 3 4 5 6 7] | |
(map dec) | |
(filter even?) | |
(filter #(> % 2)) | |
(reduce +))) | |
=> #'user/run-lazyseqs | |
(time (dotimes [_ (range 10000)] (run-handwritten))) | |
java.lang.ClassCastException: clojure.lang.LongRange cannot be cast to java.lang.Number | |
(time (dotimes [_ 10000] (run-handwritten))) | |
"Elapsed time: 12.271052 msecs" | |
=> nil | |
(time (dotimes [_ 1000000] (run-handwritten))) | |
"Elapsed time: 73.239849 msecs" | |
=> nil | |
(time (dotimes [_ 100000000] (run-handwritten))) | |
"Elapsed time: 6528.968932 msecs" | |
=> nil | |
(time (dotimes [_ 10000000] (run-handwritten))) | |
"Elapsed time: 650.94608 msecs" | |
=> nil | |
(time (dotimes [_ 10000000] (run-transducer))) | |
"Elapsed time: 1454.512474 msecs" | |
=> nil | |
(time (dotimes [_ 10000000] (run-lazyseqs))) | |
"Elapsed time: 7126.864204 msecs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment