Defining reduced arity partial functions
clojure, functional-programming, lisp
Solution
In languages where functions are curried by default, the function calls are also curried. When one writes `(f a b c)`, the language interprets this as `(((f a) b) c)`. This is not the case in Clojure.
I believe that making curried functions in an environment where calls are not curried by default creates a conceptual mismatch - this construct will probably cause confusion in readers (I mean human readers of your code.)
If your function has more than 2 arguments, the definition gets ugly quickly. Suppose a function has 4 arguments. To fully emulate the currying call, you need to handle cases like `((f a b) c d)` when someone first passes 2 arguments and then the remaining two. In this case the overloaded version of the two-argument function needs to return an overloaded function that behaves differently depending on whether it gets 1 or 2 arguments. I guess it's possible to automate that with macros, but still.
Also, you kill the possibility of defining default arguments and the `& rest` construct.
Problem
I've sometimes found it convenient in Clojure to define reduced arity versions of functions, that return a partial function, e.g. ``` (defn prefix ([pre string] (str pre ":" string)) ([pre] (fn [string] (prefix pre string)))) ``` This means that you can do either: ``` (prefix "foo" 78979) => "foo:78979" ((prefix "foo") 78979) => "foo:78979" ``` This seems quite Haskell-ish and avoids the need for `partial` to create partial functions. But is it considered good coding style / API design in Lisp?