Perl may have coined TMTOWTDI (there’s more than one way to do it) but Clojure takes that idea and runs very far with it.
Today I’d like to quickly show you four ways to create a function in Clojure.
Each block of code will define a
doubler function that simply doubles its given integer argument.
This is essentially the standard way to define a function in Clojure: name, args, function body.
(defn doubler [n] (* 2 n))
(def doubler (fn [n] (* 2 n)))
This is a verbose variant of
defn and is essentially what
defn is doing behind the scenes. Here we have an anonymous function (the
fn bound to a name with
Clojure also allows defining functions with an even shorter syntax than
# character can define function literals. With that syntax you can’t have named arguments but instead get positional references e.g.
%1 %2 %3
(def doubler #(* 2 %1))
Now it gets interesting. Clojure provides the most wonderful and excellent
partial function. The
partial function accepts a function and some arguments and returns a new function that calls the original function with those originally given arguments plus any remaining required arguments.
Ah it’s hard to explain so I’ll show it: this is
(def doubler (partial * 2))
What’s happening there?
def doubler is simply assigning the name so let’s ignore that.
The real interesting bit
(partial * 2)
That call to
partial accepts a function
* and an argument
2 and returns a new function that will accept more arguments to supply to the
(* 2) call.
The new function takes any arguments it’s given and supplies them to
(* 2) as though we had called it directly.
(def doubler (partial * 2)) (doubler 10) ; => 20 == (* 2 10) (doubler 3 4) ; => 24 == (* 2 3 4)
You can see it’s not quite the same as the first two
doubler functions in that it takes any number of arguments. Such is the power and flexibility of