Skip to content

Commit 4873c39

Browse files
committed
Merge pull request bbatsov#110 from arrdem/master
[bbatsov#109] Add advisory for sorting arities
2 parents 09da192 + 325ff04 commit 4873c39

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,40 @@ when there are no arguments on the same line as the function name.
259259
(foo x 1))
260260
([x y]
261261
(+ x y)))
262+
```
263+
264+
* <a name="multiple-arity-order"></a> Sort the arities of a function
265+
from fewest to most arguments. The common case of multi-arity
266+
functions is that some K arguments fully specifies the function's
267+
behavior, and that arities N < K partially apply the K arity, and
268+
arities N > K provide a fold of the K arity over varargs.
269+
<sup>[[link](#multiple-arity-order)]</sup>
270+
271+
```Clojure
272+
;; good - it's easy to scan for the nth arity
273+
(defn foo
274+
"I have two arities."
275+
([x]
276+
(foo x 1))
277+
([x y]
278+
(+ x y)))
279+
280+
;; okay - the other arities are applications of the two-arity
281+
(defn foo
282+
"I have two arities."
283+
([x y]
284+
(+ x y))
285+
([x]
286+
(foo x 1))
287+
([x y z & more]
288+
(reduce foo (foo x (foo y z)) more)))
289+
290+
;; bad - unordered for no apparent reason
291+
(defn foo
292+
([x] 1)
293+
([x y z] (foo x (foo y z)))
294+
([x y] (+ x y))
295+
([w x y z & more] (reduce foo (foo w (foo x (foo y z))) more)))
262296
```
263297

264298
* <a name="align-docstring-lines"></a>

0 commit comments

Comments
 (0)