As it turns out you shouldn't listen to me that much. I messed up completely
with my previous post „Pointfree vs. Pointless“. As Chouser points
out: My claims about partial
and comp
creating classes are totally wrong.
Here's why…
What does partial
actually do. It takes a function and some arguments and
returns another function, which when called appends its arguments to the given
ones and passes everything on to the given function. So this
(let [part-f (partial f :a :b)]
(part-f :c :d))
is basically equivalent to
(f :a :b :c :d)
Another way to achieve this, is
(let [sharp-f #(f :a :b %1 %2)]
(sharp-f :c :d))
So the #()
reader macro can be used to implement partial
.
(defn partial
[f & args]
#(apply f (concat args %&)))
Question: What is the difference between part-f
and sharp-f
? Right.
part-f
does not create a new class while sharp-f
does.
The definition of partial
creates actually two classes: one for partial
itself and one for the returned function. However, the second class is always
the same! For all calls to partial
! So it actually does not create a new
class when it's called, but only an instance of the second class.
Now the #()
version on the other hand is actually a reader macro expanding
into a fn
form. This actually does create a new class each time it is used!
So five #()
s create five new classes, while five partial
s create only five
instances of the same class.
Be humble. Understand before you start blogging.
Published by Meikel Brandmeyer on .
I'm a long-time Clojure user and the developer of several open source projects mostly involving Clojure. I try to actively contribute to the Clojure community.
My most active projects are at the moment VimClojure, Clojuresque and ClojureCheck.
Copyright © 2009-2014 All Right Reserved. Meikel Brandmeyer