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
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#
msgid ""
msgstr ""
msgid "title: Clojure auto curry"
msgstr ""
msgid "date: 2021-04-24"
msgstr ""
msgid "layout: post"
msgstr ""
msgid "lang: en"
msgstr ""
msgid "ref: clojure-auto-curry"
msgstr ""
msgid ""
"(defmacro defcurry\n"
" [fname args & body]\n"
" (let [partials (map (fn [n]\n"
" `(~(subvec args 0 n) (partial ~fname ~@(take n args))))\n"
" (range 1 (count args)))]\n"
" `(defn ~fname\n"
" (~args ~@body)\n"
" ~@partials)))\n"
msgstr ""
msgid "A naive `add` definition, alongside its usage and macroexpansion:"
msgstr ""
msgid ""
"user=> (defcurry add\n"
" [a b c d e]\n"
" (+ 1 2 3 4 5))\n"
"#'user/add\n"
"\n"
"user=> (add 1)\n"
"#object[clojure.core$partial$fn__5857 0x2c708440 \"clojure.core$partial$fn__5857@2c708440\"]\n"
"\n"
"user=> (add 1 2 3 4)\n"
"#object[clojure.core$partial$fn__5863 0xf4c0e4e \"clojure.core$partial$fn__5863@f4c0e4e\"]\n"
"\n"
"user=> ((add 1) 2 3 4 5)\n"
"15\n"
"\n"
"user=> (((add 1) 2 3) 4 5)\n"
"15\n"
"\n"
"user=> (use 'clojure.pprint)\n"
"nil\n"
"\n"
"user=> (pprint\n"
" (macroexpand\n"
" '(defcurry add\n"
" [a b c d e]\n"
" (+ 1 2 3 4 5))))\n"
"(def\n"
" add\n"
" (clojure.core/fn\n"
" ([a b c d e] (+ 1 2 3 4 5))\n"
" ([a] (clojure.core/partial add a))\n"
" ([a b] (clojure.core/partial add a b))\n"
" ([a b c] (clojure.core/partial add a b c))\n"
" ([a b c d] (clojure.core/partial add a b c d))))\n"
"nil\n"
msgstr ""
msgid ""
"This simplistic `defcurry` definition doesn't support optional parameters, "
"multi-arity, `&` rest arguments, docstrings, etc., but it could certainly "
"evolve to do so."
msgstr ""
msgid ""
"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."
msgstr ""
msgid "Same Clojure as before, now with auto-currying via macros."
msgstr ""
msgid ""
"Here's a simple macro defined by [Loretta "
"He](http://lorettahe.github.io/clojure/2016/09/22/clojure-auto-curry) to "
"create Clojure functions that are curried on all arguments, relying on "
"Clojure's multi-arity support:"
msgstr ""
#~ msgid ""
#~ "A simple macro defined by [Loretta "
#~ "He](http://lorettahe.github.io/clojure/2016/09/22/clojure-auto-curry) to "
#~ "create Clojure functions that are curried on all arguments, relying on "
#~ "Clojure's multi-arity support:"
#~ msgstr ""
#~ msgid "Comparison with Common Lisp"
#~ msgstr ""
#~ msgid "My attempt at writing an equivalent for Common Lisp gives me:"
#~ msgstr ""
#~ msgid ""
#~ "Without built-in multi-arity support, we have to do more work, like tracking"
#~ " the number of arguments consumed so far. That is, without dependending on "
#~ "any library, sticking to ANSI Common Lisp."
#~ msgstr ""
#~ msgid ""
#~ "This also require `funcall`s, since we return a `lambda` that doesn't live "
#~ "in the function namespace."
#~ msgstr ""
#~ msgid ""
#~ "Like the Clojure one, it doesn't support optional parameters, `&rest` rest "
#~ "arguments, docstrings, etc., but it also could evolve to do so."
#~ msgstr ""
#~ msgid ""
#~ "(defun curry-n (n fn)\n"
#~ " (if (= 0 n)\n"
#~ " (funcall fn)\n"
#~ " (lambda (&rest rest)\n"
#~ " (curry-n (something n) fn))))\n"
#~ "\n"
#~ "(defun add (a b c d e)\n"
#~ " (curry-n\n"
#~ " (length '(a b c d e))\n"
#~ " (lambda (&rest rest)\n"
#~ " (apply #'+ rest))))\n"
#~ msgstr ""
#~ msgid ""
#~ "(defun curry-n (n fn)\n"
#~ " (if (= 0 n)\n"
#~ " (funcall fn)\n"
#~ " (lambda (&rest rest)\n"
#~ " (curry-n (something n) fn))))\n"
#~ "\n"
#~ "(defun add (a b c d e)\n"
#~ " (curry-n\n"
#~ " (length '(a b c d e))\n"
#~ " (lambda (&rest rest)\n"
#~ " (apply #'+ rest))))\n"
#~ msgstr ""
|