Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created December 19, 2015 20:32
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 domgetter/f50aa2cda6d4f8b0c6eb to your computer and use it in GitHub Desktop.
Save domgetter/f50aa2cda6d4f8b0c6eb to your computer and use it in GitHub Desktop.
;; The tranducer-returning arity of map does as follows:
(defn map [f]
(fn [rf]
(fn
([] (rf))
([result] (rf result))
([result input]
(rf result (f input)))
([result input & inputs]
(rf result (apply f input inputs))))))
;; So calling (map inc) should return the following:
(fn [rf]
(fn
([] (rf))
([result] (rf result))
([result input]
(rf result (inc input)))
([result input & inputs]
(rf result (apply inc input inputs)))))
;; Let's call it map-inc
(def map-inc
(fn [rf]
(fn
([] (rf))
([result] (rf result))
([result input]
(rf result (inc input)))
([result input & inputs]
(rf result (apply inc input inputs))))))
;; If I call (map-inc +), then I should get the following:
(fn
([] (+))
([result] (+ result))
([result input]
(+ result (inc input)))
([result input & inputs]
(+ result (apply inc input inputs))))
;; Let's call this inc-then-add
(def inc-then-add
(fn
([] (+))
([result] (+ result))
([result input]
(+ result (inc input)))
([result input & inputs]
(+ result (apply inc input inputs)))))
;; This works for a while...
(inc-then-add) ;=> 0
(inc-then-add 1) ;=> 1
(inc-then-add 1 2) ;=> 4
(inc-then-add 1 2 3) ;=> ArityException Wrong number of args (2) passed to: core/inc
;; Hmm, it looks like it's trying to call (apply inc 2 '(3)), which gives the same error:
(apply inc 2 '(3)) ;=> ArityException Wrong number of args (2) passed to: core/inc
;; So why the heck does this work:
(transduce (map inc) + '(1 2 3)) ;=> 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment