ClojureDocs

Nav

Namespaces

identity

clojure.core

Available since 1.0 (source)
  • (identity x)
Returns its argument.
8 Examples
user=> (identity 4)
4
user=> (filter identity [1 2 3 nil 4 false true 1234])
(1 2 3 4 true 1234)
user=> (map #(%1 %2) (cycle [inc identity]) [1 2 3 4 5 6 7 8 9 10])
(2 2 4 4 6 6 8 8 10 10)
user=> (partition-by identity (sort "abcdaabccc"))
((\a \a \a) (\b \b) (\c \c \c \c) (\d))
user=> (map first (partition-by identity [1 1 2 3 3 1 1 5 5]))
(1 2 3 1 5)
user=> (group-by identity "abracadabra")
{\a [\a \a \a \a \a], \b [\b \b], \r [\r \r], \c [\c], \d [\d]}
user=> (map #(identity %) [1 2 3 4]) ; ~ (map (fn [x] x) [1 2 3 4])
(1 2 3 4)
; `identity` can serve in workarounds, because you can't pass a macro
; to a function. For example, you can't pass `and` as a parameter to `apply`:
(apply and '(true 1 "yes"))
; \=> CompilerException... Can't take value of a macro...

; Instead:
(every? identity '(true 1 "yes"))
See Also

Returns true if x is nil, false otherwise.

Added by boxie

Returns true if x is not nil, false otherwise.

Added by timgilbert

Returns a function that takes any number of arguments and returns x.

Added by Ramblurr
4 Notes
    By , created 14.7 years ago

    I don't quite see the usefulness of this :P

    By , created 13.7 years ago, updated 13.7 years ago

    It's useful for example with -> macro when we eventually want to return its argument (in this case: state)

    (defn example[state] (-> state update-function-1 update-function-2 identity))

    By , created 13.6 years ago

    Here is another good example:

    (some identity ((juxt :foo :bar) {:bar :b}))
    equivalent to

     (let [map {:bar b}] (or (:foo map) (:bar map)))

    By , created 10.2 years ago

    user=> (mapcat identity [[[0 1] [1 2]] [[11 12]]]) ([0 1] [1 2] [11 12])