Clojure: Arity Overloading

Coming back to Clojure again. It’s been so long, let’s start from the beginning.

;; Arity Overloading
(defn sum
  "Add two numbers or a list of numbers and return result."
  ;; add two numbers
  ([a b]
    (+ a b))
  ;; add list of numbers
  ([x]
    (reduce + x)))

(sum 1 2)                                                   ;=> 3
(sum (range 10))                                            ;=> 45

—addition—

Give Carbon a try. Cool.

Sidenote

I just realized that with IntelliJ and Cursive IDE, there’s no need to install additional JDK and Leiningen in order to write Clojure. That’s good.