def partial vs let partial

118 views
Skip to first unread message

Matthew Hamrick

unread,
Dec 1, 2016, 4:05:46 PM12/1/16
to Clojure
I'm confused by the following code.
Could someone explain to me why the def-ed partial has different behavior to the letted one?
This is especially confusing to me since the #() special form one works as I expect.

(def sum-partial-def (partial reduce +))

(let [sum-partial (partial reduce +)
      sum-# #(reduce + %1)
      nums [1 2 3 4]]
  [(sum-partial-def nums)
   (reduce + nums)
   (sum-# nums)
   (sum-partial nums)]) ;; => [10 10 10 10]

(with-redefs [+ (fn [a b]
                  (.add (.add (BigInteger. (str a))
                              (BigInteger. (str b)))
                        (BigInteger/ONE)))]
  (let [sum-partial (partial reduce +)
        sum-# #(reduce + %1)
        nums [1 2 3 4]]
    [(sum-partial-def nums)
     (reduce + nums)
     (sum-# nums)
     (sum-partial nums)])) ;; => [10 13 13 13]

Thanks,
Matt

Gary Trakhman

unread,
Dec 1, 2016, 4:14:38 PM12/1/16
to clo...@googlegroups.com

Sum-partial-def gets the original + definition because it is evaluated first, if you want late binding, try (partial reduce (var +)).


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Timothy Baldridge

unread,
Dec 1, 2016, 4:19:26 PM12/1/16
to clo...@googlegroups.com
It's because the value of the + is captured when the partial is created (or when the var is implicitly derefed). The value of the var is implicitly captured (via deref) at the point where it appears in the form. 

It's a bit of a complex topic, but this blog post I wrote a few months ago may help a bit: http://blog.cognitect.com/blog/2016/9/15/works-on-my-machine-understanding-var-bindings-and-roots

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)
Reply all
Reply to author
Forward
0 new messages