Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure `defn` parameter in parentheses or not

Tags:

clojure

I saw a Clojure function is defined as

(defn toInt([i] (Integer. i)))

why the parameter [i] is included in parentheses? is this the same as below? any differences?

 (defn toInt [i] (Integer. i)) 
like image 327
ahala Avatar asked Jan 18 '14 19:01

ahala


Video Answer


1 Answers

The first uses the notation for arity overloading, but contains only one arity.

Example with two arities:

(defn my-add 
  ([x] (+ x 1))
  ([x y] (+ x y)))

(my-add 1) ;;=> 2
(my-add 1 2) ;;=> 3

Also see http://clojure.org/functional_programming (search for arity overloading).

like image 52
Michiel Borkent Avatar answered Nov 15 '22 08:11

Michiel Borkent