I am trying to understand transducers and am reading through 
https://clojure.org/reference/transducers I think I am missing something, 
please can someone help?

I read the part about the 'shape' of a transducer-creating-function (
https://clojure.org/reference/transducers#_creating_transducers) and made 
this function

(defn no-op [rf]
  (fn ([] (rf))
    ([x] (rf x))
    ([x r] (rf x r))))

which I call with

(pprint (eduction no-op (range 5)))

and get 

(0 1 2 3 4)

This is OK. If I now define 

(defn nearly-no-op [rf]
  (fn ([] (rf))
    ([x] (let [tmp (rf x)] (* x 2)))
    ([x r] (rf x r))))

(I want the 'finishing' function to double the answer)

Then (pprint (eduction nearly-no-op (range 5))) gives me a null pointer 
exception in multiply. Where does this come from? 

If I try

(defn nearly-no-op [rf]
  (fn ([] (rf))
    ([x] (count (rf x)))
    ([x r] (rf x r))))

then I get (0 1 2 3 4) again, i.e. the count doesn't play any part in the 
final result.

Thanks in advance!

Complete program:
(ns ttry.core
  (:require [clojure.pprint :refer [pprint]])
  (:gen-class))

(defn no-op [rf]
  (fn ([] (rf))
    ([x] (rf x))
    ([x r] (rf x r))))

(defn nearly-no-op [rf]
  (fn ([] (rf))
    ([x] (let [tmp (rf x)] (* x 2)))
    ([x r] (rf x r))))

(defn -main
  [& args]
  (pprint (eduction (map identity) (range 5)))
  (pprint (eduction no-op (range 5)))
  (pprint (eduction nearly-no-op (range 5))))






-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to