Introduction to clojure

450 views

Published on

A brief introduction to Clojure language.

Published in: Technology
  • Be the first to comment

Introduction to clojure

  1. 1. Introduction to Clojure By Abhishek Mahawar BM Tech Talk
  2. 2. 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).
  3. 3. Clojure is a …. ● LISP dialect targeting the JVM ● Dynamic programming language ● Functional programming language ● Compiled language
  4. 4. Clojure is a LISP ● S-expressions (Polish notation) (+ 2 4) ● Homoiconicity code is data, data is code ● Reader Operator/function names Arguments
  5. 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. 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. 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. 8. 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
  9. 9. Clojure Evaluation Clojure code Reader characters evaluator/ compiler data structures JVM Output bytecode
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 13. Lots of other stuff ● Software Transactional Memory ● Macros ● Lazy Sequences ● Metadatas
  14. 14. References ● http://clojure.org/ ● http://www.youtube.com/clojuretv ● Clojure Programming by Chas Emerick, Brian Carper and Christophe Grand

×