Skip to content

Instantly share code, notes, and snippets.

@ghadishayban
Last active January 26, 2019 04:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghadishayban/cf7ca283ccb96321526f570ccf83b94a to your computer and use it in GitHub Desktop.
Save ghadishayban/cf7ca283ccb96321526f570ccf83b94a to your computer and use it in GitHub Desktop.
"stream fusion" example using transducers
;; 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