Skip to content

Instantly share code, notes, and snippets.

@minimal
Last active August 29, 2015 14:10
Show Gist options
  • Save minimal/e9ea9b6ce8a473b468a8 to your computer and use it in GitHub Desktop.
Save minimal/e9ea9b6ce8a473b468a8 to your computer and use it in GitHub Desktop.
(ns transducers-exp.core
(:require [criterium.core :as cr])
(:gen-class))
(def xform (comp (map inc)
(filter even?)
(dedupe)
(mapcat range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(mapcat flatten)
(random-sample 1.0)
(take-nth 1)
(keep #(when (odd? %) (* % %)))
(keep-indexed #(when (even? %1) (* %1 %2)))
(replace {2 "two" 6 "six" 18 "eighteen"})
(take 11)
(take-while #(not= 300 %))
(drop 1)
(drop-while string?)
(remove string?)))
(def data (vec (interleave (range 18) (range 20))))
(defn thread-form [d]
(->> d
(map inc)
(filter even?)
(dedupe)
(mapcat range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(mapcat flatten)
(random-sample 1.0)
(take-nth 1)
(keep #(when (odd? %) (* % %)))
(keep-indexed #(when (even? %1) (* %1 %2)))
(replace {2 "two" 6 "six" 18 "eighteen"})
(take 11)
(take-while #(not= 300 %))
(drop 1)
(drop-while string?)
(remove string?)))
(def xform-small (comp (map inc)
(filter even?)
(dedupe)
(mapcat range)
(partition-all 3)
(partition-by #(< (apply + %) 7))))
(defn thread-small [d]
(->> d
(map inc)
(filter even?)
(dedupe)
(mapcat range)
(partition-all 3)
(partition-by #(< (apply + %) 7))))
(def xform-micro (comp (map inc)
(filter even?)))
(defn thread-micro [d]
(->> d
(map inc)
(filter even?)))
(def xform-micro2 (comp (dedupe)
(replace {2 "two" 6 "six" 18 "eighteen"})))
(defn thread-micro2 [d]
(->> d
(dedupe)
(replace {2 "two" 6 "six" 18 "eighteen"})))
(comment
(cr/with-progress-reporting (cr/bench (sequence xform data)))
(cr/with-progress-reporting (cr/bench (thread-form data)))
(cr/with-progress-reporting (cr/bench (transduce xform + data)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment