aboutsummaryrefslogtreecommitdiff
path: root/locale/pt/LC_MESSAGES/_tils/2021-04-24-clojure-auto-curry.po
blob: ab59a4fa19d80c3d4a3e4dac59c868806347af09 (about) (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#
msgid ""
msgstr ""

msgid "title: Clojure auto curry"
msgstr ""

msgid "layout: post"
msgstr ""

msgid "lang: en"
msgstr ""

msgid "ref: clojure-auto-curry"
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 "date: 2021-04-24 1"
msgstr ""

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

msgid "Comparison with Common Lisp"
msgstr ""

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

msgid ""
"(defun partial (fn &rest args)\n"
"  (lambda (&rest args2)\n"
"    (apply fn (append args args2))))\n"
"\n"
"(defun curry-n (n func)\n"
"  (cond ((< n 0) (error \"Too many arguments\"))\n"
"        ((zerop n) (funcall func))\n"
"        (t (lambda (&rest rest)\n"
"             (curry-n (- n (length rest))\n"
"                      (apply #'partial func rest))))))\n"
"\n"
"(defmacro defcurry (name args &body body)\n"
"  `(defun ,name (&rest rest)\n"
"     (let ((func (lambda ,args ,@body)))\n"
"       (curry-n (- ,(length args) (length rest))\n"
"                (apply #'partial func rest)))))\n"
msgstr ""

msgid ""
"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."
msgstr ""

msgid "The usage is pretty similar:"
msgstr ""

msgid ""
"* (defcurry add (a b c d e)\n"
"    (+ a b c d e))\n"
"ADD\n"
"\n"
"* (add 1)\n"
"#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216419B}>\n"
"\n"
"* (funcall (add 1) 2 3 4)\n"
"#<FUNCTION (LAMBDA (&REST REST) :IN CURRY-N) {100216537B}>\n"
"\n"
"* (funcall (add 1) 2 3 4 5)\n"
"15\n"
"\n"
"* (funcall (funcall (add 1) 2 3) 4 5)\n"
"15\n"
"\n"
"* (macroexpand-1\n"
"    '(defcurry add (a b c d e)\n"
"       (+ a b c d e)))\n"
"(DEFUN ADD (&REST REST)\n"
"  (LET ((FUNC (LAMBDA (A B C D E) (+ A B C D E))))\n"
"    (CURRY-N (- 5 (LENGTH REST)) (APPLY #'PARTIAL FUNC REST))))\n"
"T\n"
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 "updated_at: 2021-04-27"
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 "date: 2021-04-24"
#~ 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 ""
#~ "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 ""
#~ "(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 ""