I wrote a question, but I think my point was not clear.
I wanted some trace output of my codes, but my code uses heavily liked
structure.
After I found out there are vars that controls output, I tried that.
Let's consider below codes. (I made runnable example.)
(The main differenece from real code and example is that
- printing of args/returns done by "trace" library, not by the
'pmaped' function itself)
- Not much relation to lazy things.
- Not much relation to closure.
;; BEGIN -------------------------------
;; this is *EXAMPLE* codes.
;; So don't ask things like why pass a range instead of a number.
(defn pmaped [r]
(println "pmaped - called with - " r) ;; think as trace output
(Thread/sleep (rand 200))
(let [ret (reduce + r)]
(println "pmaped returns - " ret)
ret))
(defn some-fn
([] (some-fn 100))
([n]
(let [square (fn [x] (* x x))
rs (map range (range n))]
(reduce + (pmap pmaped rs)))))
;; END ---------------------------------
; Consider this
(binding [*print-length* 2]
(some-fn 10))
; Some time needed to figure out why above code does not work as I
expected.
; That's because I used pmap.
; Environment of threads created(or picked in pool) by pmap do not
keep that of caller.
; But am I wrong to expect pmap to behave similar to map?
; Yes I expect order of side effects differs, but not the effects
itself.
;; My (non-)solution -
(binding [*print-length* 2
pmap map] ;; ^^
(some-fn 10))
;; But how about this? (borrowed from another group thread.)
(use 'clojure.contrib.duck-streams)
(defmacro with-out-as
[f & body]
`(with-open [w# (writer ~f)]
(binding [*out* w#]
~...@body)))
(with-out-as "/tmp/trace.out"
(some-fn 10))
; That means if I can use macros like above,
; I have to be sure all called functions do not use multi-threading
facilities,
; which is not practical.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---