Clojure Data Types :

Type

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 Use class to inspect clojure expressions.

Numbers:


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

BigInt

  ; an arbitrary-precision integer

  (class 3N)
  ; => clojure.lang.BigInt

Integer

  ; 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

Double

  ; use 64 bits

  (type 3.14)
  ; => java.lang.Double

Float

  ; use 32 bits

  (type (float 3.14))
  ; => java.lang.Float

Ratios

  ;Represents a ratio between integers.

  (type 22/7)
  ; => clojure.lang.Ratio

Strings:


  (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)
  ; => ""

Booleans


Every value in cojure is true except false and nil (both return false) (just like ruby)

  (type true)
  ; => java.lang.Boolean

Symbols


Not to be confused with symbols in Ruby

Symbols in clojure are used to name things. Like: Functions like str and inc

To create a symbol just use quoting i.e:

  '(+ 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.

Keywords


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