liveBook · Manning

Arity

As explained in Clojure: The Essential Reference

An arity is a function signature defined by the number of arguments it accepts.

An arity is a function signature distinguished by how many arguments it accepts. In Clojure, a function can declare more than one arity, giving different calls different behavior without requiring separate function names. section 2.1.2

Declaring two arities

((fn ([] "a") ([x] "x"))) ;=> "a" ((fn ([] "a") ([x] "x")) 42) ;=> "x" Each parameter vector starts an arity: `[]` accepts no arguments, while `[x]` accepts one. With just one arity, `fn` can omit the parentheses around that declaration, as in `(fn [x] x)`. section 2.1.2

The same distinction matters when calling familiar arithmetic functions. Zero arguments produce identity values for `+` and `*`, but that arity is unavailable for `-` and `/`. With one argument, `+` and `*` return it, `-` changes its sign, and `/` returns its reciprocal. section 5.5

An empty collection

(apply + []) ;=> 0 (apply * []) ;=> 1 Because `apply` can call these functions with no arguments, their zero-argument arities give useful results even when the input collection is empty. section 5.5

Arity is about the argument count, not the types of the arguments or the value returned. Check each declared arity when choosing how to call a function—especially for zero- and one-argument cases. section 2.1.2 section 5.5

How Clojure: The Essential Reference covers it

How deep it goes
Defines arity through parameter vectors and works through arithmetic edge cases; does not explain function dispatch internals.
Examples
Shows a two-arity anonymous function, a recursive Fibonacci function, and arithmetic identities applied to an empty collection.
What it assumes
Introduces arity from fn parameter vectors while using basic function calls and arithmetic as context.
What you can do after
Build it from scratch with multiple fn signatures and evaluate arithmetic calls by their argument count.
How it connects
Connects arity to anonymous functions, parameter vectors, recursion, arithmetic operators, and apply.

Also explained in

Compare how 5 books explain arity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.