Created
July 7, 2012 23:22
-
-
Save gigasquid/3068538 to your computer and use it in GitHub Desktop.
Baba Yaga Reducers
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
(ns reducers.core | |
(:require [clojure.core.reducers :as r])) | |
(map + [1 2 3]) ;=> (1 2 3) | |
(class(map + [1 2 3])) ;=> (1 2 3) | |
(def odyssey-text (slurp "odyssey.txt")) | |
(class odyssey-text) ;=> java.lang.String | |
(first odyssey-text) ;=> \P | |
(class (first odyssey-text)) ;=> java.lang.Character | |
(.hashCode (first odyssey-text)) ;=> 80 | |
(.hashCode (second odyssey-text)) ;=> 114 | |
(.hashCode (nth odyssey-text 2)) ;=> 111 | |
(defn hashcode [c] (.hashCode c)) | |
(map hashcode odyssey-text) ;=> (80 114 111..... ) | |
(filter even? (map hashcode odyssey-text)) ;=> (80 114 ... ) | |
(reduce + (filter even? (map hashcode odyssey-text))) ;=> 33702446 | |
(dotimes [n 5] | |
(println (str "map - filter - reduce - ( run " n " ):")) | |
(time (reduce + (filter even? (map hashcode odyssey-text))))) | |
(reduce + (r/filter even? (r/map hashcode odyssey-text))) ;=> 33702446 | |
(dotimes [n 5] | |
(println (str "r/map - r/filter - reduce - ( run " n " ):")) | |
(time (reduce + (r/filter even? (r/map hashcode odyssey-text))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment