-
-
Save jean-lopes/1968fc27dacb59eac8494c00f79daa7f to your computer and use it in GitHub Desktop.
Transducer debug
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 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