Skip to content

Instantly share code, notes, and snippets.

@jean-lopes
Created October 24, 2022 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jean-lopes/1968fc27dacb59eac8494c00f79daa7f to your computer and use it in GitHub Desktop.
Save jean-lopes/1968fc27dacb59eac8494c00f79daa7f to your computer and use it in GitHub Desktop.
Transducer debug
(ns sample
(:require [clojure.pprint :refer [pprint]]))
(def tap-fn
pprint)
(add-tap #'tap-fn)
(defn xdebug [xf]
(fn [rf]
(let [f (xf rf)]
(fn
([]
(tap> {:init {}})
(f))
([acc]
(tap> {:complete {:acc acc}})
(f acc))
([acc value]
(tap> {:step (array-map :acc acc :value value)})
(f acc value))))))
(def xf
(xdebug (map inc)))
(into [] xf (range 5))
;; => [1 2 3 4 5]
;; prints:
;; {:step
(sequence xf (range 5))
;; => (1 2 3 4 5)
;; prints:
;; {:step {:acc nil, :value 0}}
;; {:step {:acc nil, :value 1}}
;; {:step {:acc nil, :value 2}}
;; {:step {:acc nil, :value 3}}
;; {:step {:acc nil, :value 4}}
;; {:complete {:acc nil}}
(transduce xf + (range 5))
;; => 15
;; {:step {:acc 0, :value 0}}
;; {:step {:acc 1, :value 1}}
;; {:step {:acc 3, :value 2}}
;; {:step {:acc 6, :value 3}}
;; {:step {:acc 10, :value 4}}
;; {:complete {:acc 15}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment