ClojureDocs

Nav

Namespaces

inc

clojure.core

Available since 1.2 (source)
  • (inc x)
Returns a number one greater than num. Does not auto-promote
longs, will throw on overflow. See also: inc'
4 Examples
user=> (inc 1)
2

user=> (inc 1.0)
2.0

user=> (inc 1/2)
3/2

user=> (inc -1)
0
;;increment all the element in a collection

(map inc [1 2 3 4 5])
;;(2 3 4 5 6)  return type list

(into [] (map inc [1 2 3 4 5]))
;;[2 3 4 5 6]  return type vector
;; Careful when using ClojureScript
;; Make sure you're passing a number
;; because JavaScript + also does string concatenation
cljs.user=> (inc "1")
"11"
cljs.user=> (inc 1)
2

;; Although it's not the case with dec
;; In JavaScript "1" - 1 = 0
cljs.user=> (dec "1")
0
cljs.user=> (dec 1)
0

;; You cannot `inc` a `nil` in Clojure, but you
;; can in ClojureScript

;; Clojure
(inc nil) ;;=> NullPointerException

;; ClojureScript
(inc nil) ;;=> 1
See Also

Returns a number one less than num. Does not auto-promote longs, will throw on overflow. See also:...

Added by frangio

Returns a number one greater than num. Supports arbitrary precision. See also: inc

Added by rvlieshout

Returns a number one greater than x, a long. Note - uses a primitive operator subject to overflow.

Added by bfontaine
3 Notes
    By , created 13.5 years ago

    Is the documentation suppose to be: "Returns a number one greater than x." ? If not what is num?

    By , created 9.1 years ago

    is code "(def i (inc i))" is conflict with the notion of immutable of data in Clojure.

    By , created 9.0 years ago

    Nope, quite the opposite. "def" defines a var:

    user=> (type (def i 1))
    clojure.lang.Var
    
    A var is one of the 4 types dealing with mutable state (it is not an immutable data structure, like maps, vectors, sets and so on) and it does so following a specific semantic (see http://clojure.org/reference/vars).