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

On Dec 1, 2016 4:05 PM, "Matthew Hamrick" <matthewjhamr...@gmail.com> wrote:

> 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
>
> --
> 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.
>

-- 
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