Clojure Data Types :⌗
A type is property some values share to collect all similar things. Every language has a type system. Some langugages have strict type system, some are relaxed.
Clojure is a dynamic and strongly typed language:
Dynamic - As the type checking is enforced on the fly/run time.
Strong - As operations on improper types are not allowed and errored out.
type function in clojure helps knowing what is the type of the expression. or
class to inspect clojure expressions.
Long (default) 64 bits
; as clojure is built on top of Java, many of its types are old java types (class 3) ; => java.lang.Long
Thanks to More Clojure Basic Types for more detailed insight in few others like
; an arbitrary-precision integer (class 3N) ; => clojure.lang.BigInt
; uses 32 bits (class (int 1)) ; => java.lang.Integer
Short & Byte
; Short use 16 bits (class (short 3)) ; => java.lang.Short ; Byte use 8 bits (type (byte 0)) ; => java.lang.Byte
; use 64 bits (type 3.14) ; => java.lang.Double
; use 32 bits (type (float 3.14)) ; => java.lang.Float
;Represents a ratio between integers. (type 22/7) ; => clojure.lang.Ratio
(type "Welcome to clojure") ; => java.lang.String
Interesting thing about clojure is that it only allows
"" instead of
; when using "" (println "Welcome to clojure") ; => Welcome to clojure
; usage of ' ' (println 'Welcome to clojure') ; => CompilerException java.lang.RuntimeException: Unable to resolve symbol: to in this context, compiling (NO_SOURCE_PATH:1:1)
We transform anything into a string with
str function defined by clojure
(str 1) ; => "1"
(str true) ; => "true"
(str nil) ; => ""
Every value in cojure is
nil (both return
(just like ruby)
(type true) ; => java.lang.Boolean
Not to be confused with symbols in Ruby
Symbols in clojure are used to name things. Like:
To create a symbol just use
'(+ 1 2) ; => (+ 1 2) (class '(+ 1 2)) ; => clojure.lang.Symbol
; We can evaluate symbols: (eval '(+ 1 2)) ; => 3
Symbols cannot start with a number. Job of symbols is to refer to things, to point to other values.
Seem similar to ruby symbols
Keywords are like symbols, except that keywords begin with a colon (:).They are specifically intended for use as labels or identifiers and are useful as keys in maps/hashes.
Keywords resolve to themselves.
:inc ; => :inc