Introduction to Clojure
By Abhishek Mahawar
BM Tech Talk
Not so old History
● Created by Rich Hickey
● First version in 2008
● Origin of name - Wanted to involve c(c#), l
(lisp) and j(java).
Clojure is a ….
● LISP dialect targeting the JVM
● Dynamic programming language
● Functional programming language
● Compiled language
Clojure is a LISP
● S-expressions (Polish notation)
(+ 2 4)
● Homoiconicity
code is data, data is code
● Reader
Operator/function names Arguments
Atomic Data Types
● Arbitrary precision integers 123
● Doubles 1.23 , BigDecimals 1.23M
● Ratios 22/7
● Strings “bm” , Characters b
● Symbols bm , Keywords :bm
● Booleans true false, Null - nil
Data Structures
● Lists (1 2 3)
● Vectors [1 2 3]
● Maps {:a 1 :b 2}
maps are functions of their keys
● Sets #{1 2}
Data Structures (contd.)
● Clojure data structures are
○ persistent
■ previous version remains intact when modified
○ immutable
○ support proper equality semantics in their
implementation of equals
Syntax
● We’ve just seen it !!
● No text based syntax
● Data structures are the code
Homoiconicity
● Actually, syntax is in the interpretation of
data structures
Clojure Evaluation
Clojure
code
Reader
characters
evaluator/
compiler
data structures
JVM
Output
bytecode
Expressions
● Everything is an expression
● All data literals represent themselves,
except
○ symbols - looks for binding to value
○ lists - an operation form
■ (operation arguments)
■ operation can be special ops, macros or
functions
Special ops/forms
● Can have non-normal evaluation of
arguments
○ (def name value-expr)
○ (if test-expr then-expr else-expr)
■ conditional, evaluates only one of then/else
● fn, let, loop, recur, do, new, ., throw, try,
set!, quote, var, monitor-enter, monitor-
exit
Functions
● First class values
○ (def five 5)
○ (def add-1 (fn[x] (+ x 1)))
○ (add-1 five) = 6
● Higher order functions (HOF)
● fn special op is used to create functions
Lots of other stuff
● Software Transactional Memory
● Macros
● Lazy Sequences
● Metadatas
References
● http://clojure.org/
● http://www.youtube.com/clojuretv
● Clojure Programming by Chas Emerick,
Brian Carper and Christophe Grand

Introduction to clojure

  • 1.
    Introduction to Clojure ByAbhishek Mahawar BM Tech Talk
  • 2.
    Not so oldHistory ● Created by Rich Hickey ● First version in 2008 ● Origin of name - Wanted to involve c(c#), l (lisp) and j(java).
  • 3.
    Clojure is a…. ● LISP dialect targeting the JVM ● Dynamic programming language ● Functional programming language ● Compiled language
  • 4.
    Clojure is aLISP ● S-expressions (Polish notation) (+ 2 4) ● Homoiconicity code is data, data is code ● Reader Operator/function names Arguments
  • 5.
    Atomic Data Types ●Arbitrary precision integers 123 ● Doubles 1.23 , BigDecimals 1.23M ● Ratios 22/7 ● Strings “bm” , Characters b ● Symbols bm , Keywords :bm ● Booleans true false, Null - nil
  • 6.
    Data Structures ● Lists(1 2 3) ● Vectors [1 2 3] ● Maps {:a 1 :b 2} maps are functions of their keys ● Sets #{1 2}
  • 7.
    Data Structures (contd.) ●Clojure data structures are ○ persistent ■ previous version remains intact when modified ○ immutable ○ support proper equality semantics in their implementation of equals
  • 8.
    Syntax ● We’ve justseen it !! ● No text based syntax ● Data structures are the code Homoiconicity ● Actually, syntax is in the interpretation of data structures
  • 9.
  • 10.
    Expressions ● Everything isan expression ● All data literals represent themselves, except ○ symbols - looks for binding to value ○ lists - an operation form ■ (operation arguments) ■ operation can be special ops, macros or functions
  • 11.
    Special ops/forms ● Canhave non-normal evaluation of arguments ○ (def name value-expr) ○ (if test-expr then-expr else-expr) ■ conditional, evaluates only one of then/else ● fn, let, loop, recur, do, new, ., throw, try, set!, quote, var, monitor-enter, monitor- exit
  • 12.
    Functions ● First classvalues ○ (def five 5) ○ (def add-1 (fn[x] (+ x 1))) ○ (add-1 five) = 6 ● Higher order functions (HOF) ● fn special op is used to create functions
  • 13.
    Lots of otherstuff ● Software Transactional Memory ● Macros ● Lazy Sequences ● Metadatas
  • 14.
    References ● http://clojure.org/ ● http://www.youtube.com/clojuretv ●Clojure Programming by Chas Emerick, Brian Carper and Christophe Grand