Created
July 12, 2018 16:40
-
-
Save nmoya/06218f73e3ffe1f699286dc9b7748d13 to your computer and use it in GitHub Desktop.
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 transducer-meetup.core) | |
(defn noop [xf] | |
(clojure.pprint/pprint (type xf)) | |
(clojure.pprint/pprint xf) | |
(fn | |
([] (xf)) | |
([result] (xf result)) | |
([result input] (xf result input)))) | |
(defn debug | |
([] | |
(debug ">" "<")) | |
([ident] | |
(debug ident ">" "<")) | |
([in out] | |
(fn debug-transducer [xf] | |
(fn | |
([] | |
(clojure.pprint/pprint "Arity 0") | |
(xf)) | |
([result] | |
(clojure.pprint/pprint "Arity 1") | |
(clojure.pprint/pprint result) | |
(xf result)) | |
([result input] | |
(do | |
(clojure.pprint/pprint (str in " " input)) | |
(let [output (xf result input)] | |
(clojure.pprint/pprint (str out " " output)) | |
output)))))) | |
([ident in out] | |
(let [spaces (reduce str (repeat ident " "))] | |
(debug (str spaces in) (str spaces out))))) | |
(defn slow-into [to xf from] | |
(transduce xf conj to from)) | |
; (defn beg [number] | |
; (fn [xf] | |
; (fn | |
; ([] (xf)) | |
; ([result] (xf result)) | |
; ([result input] | |
; (reduce xf result (repeat number input)))))) | |
; From https://github.com/clojure/clojure/blob/clojure-1.9.0/src/clj/clojure/core.clj#L7532 | |
(defn preserving-reduced [rf] | |
(fn [a b] | |
(let [ret (rf a b)] | |
(if (reduced? ret) | |
(reduced ret) | |
ret)))) | |
; Step 2 | |
(defn beg [n] | |
(fn [rf] | |
(let [rrf (preserving-reduced rf)] | |
(fn ([] (rf)) | |
([result] (rf result)) | |
([result input] | |
(reduce rrf result (repeat n input))))))) | |
(def beg-data (list :may :i :beg :your :pardon :?)) | |
(slow-into | |
[] | |
(comp | |
(debug 0) | |
(beg 2) | |
(debug 2) | |
(take 3) | |
(debug 4)) | |
beg-data) | |
; (transduce (debug "in" "out") + (range 3)) | |
; (into [] noop (range 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment