aboutsummaryrefslogtreecommitdiff
path: root/_tils/2021-04-24-clojure-auto-curry.md
blob: c1e277f2a8d59c2865876b69fd8b3df34725a84c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
---

title: Clojure auto curry

date: 2021-04-24 1

updated_at: 2021-04-27

layout: post

lang: en

ref: clojure-auto-curry

---

Here's a simple macro defined by [Loretta He][lorettahe] to create Clojure functions that are curried on all arguments, relying on Clojure's multi-arity support:

```clojure
(defmacro defcurry
  [name args & body]
  (let [partials (map (fn [n]
                        `(~(subvec args 0 n) (partial ~name ~@(take n args))))
                      (range 1 (count args)))]
    `(defn ~name
       (~args ~@body)
       ~@partials)))
```

A naive `add` definition, alongside its usage and macroexpansion:

```clojure
user=> (defcurry add
         [a b c d e]
         (+ 1 2 3 4 5))
#'user/add

user=> (add 1)
#object[clojure.core$partial$fn__5857 0x2c708440 "clojure.core$partial$fn__5857@2c708440"]

user=> (add 1 2 3 4)
#object[clojure.core$partial$fn__5863 0xf4c0e4e "clojure.core$partial$fn__5863@f4c0e4e"]

user=> ((add 1) 2 3 4 5)
15

user=> (((add 1) 2 3) 4 5)
15

user=> (use 'clojure.pprint)
nil

user=> (pprint
        (macroexpand
         '(defcurry add
            [a b c d e]
            (+ 1 2 3 4 5))))
(def
 add
 (clojure.core/fn
  ([a b c d e] (+ 1 2 3 4 5))
  ([a] (clojure.core/partial add a))
  ([a b] (clojure.core/partial add a b))
  ([a b c] (clojure.core/partial add a b c))
  ([a b c d] (clojure.core/partial add a b c d))))
nil
```

This simplistic `defcurry` definition doesn't support optional parameters, multi-arity, `&` rest arguments, docstrings, etc., but it could certainly evolve to do so.

I like how `defcurry` is so short, and abdicates the responsability of doing the multi-arity logic to Clojure's built-in multi-arity support.
Simple and elegant.

Same Clojure as before, now with auto-currying via macros.

[lorettahe]: http://lorettahe.github.io/clojure/2016/09/22/clojure-auto-curry

## Comparison with Common Lisp

My attempt at writing an equivalent for Common Lisp gives me:

```lisp
(defun partial (fn &rest args)
  (lambda (&rest args2)
    (apply fn (append args args2))))

(defun curry-n (n func)
  (cond ((< n 0) (error "Too many arguments"))
        ((zerop n) (funcall func))
        (t (lambda (&rest rest)
             (curry-n (- n (length rest))
                      (apply #'partial func rest))))))

(defmacro defcurry (name args &body body)
  `(defun ,name (&rest rest)
     (let ((func (lambda ,args ,@body)))
       (curry-n (- ,(length args) (length rest))
                (apply #'partial func rest)))))
```

Without built-in multi-arity support, we have to do more work, like tracking the number of arguments consumed so far.
We also have to write `#'partial` ourselves.
That is, without dependending on any library, sticking to ANSI Common Lisp.

The usage is pretty similar:

```lisp
* (defcurry add (a b c d e)
    (+ a b c d e))
ADD

* (add 1)
#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216419B}>

* (funcall (add 1) 2 3 4)
#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216537B}>

* (funcall (add 1) 2 3 4 5)
15

* (funcall (funcall (add 1) 2 3) 4 5)
15

* (macroexpand-1
    '(defcurry add (a b c d e)
       (+ a b c d e)))
(DEFUN ADD (&REST REST)
  (LET ((FUNC (LAMBDA (A B C D E) (+ A B C D E))))
    (CURRY-N (- 5 (LENGTH REST)) (APPLY #'PARTIAL FUNC REST))))
T
```

This also require `funcall`s, since we return a `lambda` that doesn't live in the function namespace.

Like the Clojure one, it doesn't support optional parameters, `&rest` rest arguments, docstrings, etc., but it also could evolve to do so.