Last active
January 26, 2019 04:13
-
-
Save ghadishayban/cf7ca283ccb96321526f570ccf83b94a to your computer and use it in GitHub Desktop.
"stream fusion" example using transducers
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
;; lots of garbage | |
;; | |
(->> coll ;; original collection | |
(map #(assoc % :foo :bar)) ;; intermediate Chunked collection 1 | |
(filter :baz) ;; intermediate Chunked collection 2 | |
(map :feature) ;; intermediate Chunked collection 3 | |
(into [])) ;; reduction using #'conj over Chunked collection #3 | |
;; -- direct reduction using transducers -- | |
;; reduction over original collection using #'conj | |
;; but #'conj that has been transformed by a transducer | |
;; the transducer is an ordinary composed function | |
;; the middle argument of (into [] xf coll) | |
;; as opposed to the macro ->> above | |
;; same basic internals, but the parentheses in a different spot | |
;; 0 intermediate collections | |
;; 1 reduction | |
(into [] | |
(comp (map #(assoc % :foo :bar)) ;; | |
(filter :baz) | |
(map :feature)) | |
coll) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment