SlideShare a Scribd company logo
(Clojure Intro)
     @basav
Agenda

• Background
• What is Clojure?
• Why Clojure?
• Some code..
Clojure Vocabulary
Clojure Vocabulary
 Functional             LISP           STM
 Programming
                           lambda calculus
   recursion
                           (λ)
       prefix notation
                                        JVM
Lazy Sequences          macros

  homoiconicity     REPL          S-expressions
Who are these guys?
Jedi Masters of
Programming Taxonomy
     http://en.wikipedia.org/wiki/
Comparison_of_programming_paradigms
What is Clojure?

Clojure is a dynamic, strongly
typed, functional, high
performance implementation
of Lisp on the JVM (and CLR
and yes JavaScript)
What is Lisp?
LISP stands for LISt Processing.
Designed by John McCarthy.
Implementation of lambda
calculus.
Oldest Language - Circa 1958
Uses Lots of ( )s - Polish Prefix
notation
Known for AI, DSLs etc
Example LISP Code




http://kazimirmajorinc.blogspot.in/2012/03/few-examples-of-lisp-code-typography.html
What is lambda calculus?
• Formal system computation by
  for expressing
                 in Computer Science

    functions
• Consists of λ expressions
    •   variables v1, v2, ..., vn, ...
    •   the abstraction symbols λ and .
    •   parentheses ( )
•   Examples
•   λx.x — is a function taking an argument x,
    and returning x                                  Alonzo Church



•   f x — is a function f applied to an argument x
Clojure History
• Project led by Rich Hickey
    •   1.0 in 2007

    •   1.1 in 2009

    •   1.2 in 2010

    •   1.3 in 2011

    •   1.4 in 2012

    •   1.5 released in March 2013


•   Maintained on Github

    •   contains one jar file clojure.jar

•   You can get it from Maven repo
Why Clojure?
http://java.dzone.com/articles/results-2012-state-clojure
Q: How long have you been using Clojure?
Q: What language did you use just prior to adopting Clojure ?
Q: How would you characterize your use of
Clojure today?
Q: In which domain(s) are you using Clojure?
Q: What have been the biggest wins for you in
using Clojure?
Q: Which development environment(s) do you
use to work with Clojure?
Why Clojure?
•   Good at working with Data

    •   Rich literal types like arrays, sets, data types

•   Good at expressing algorithms

    •   Functional

•   Great for concurrency

    •   Immutable data structure

    •   Innovative approach to state - STM
Some Clojure Syntax
println("Hello world!") ;
(println "Hello world!") ;
Data literals
(1 2 3 "this" "is" :a :list)
[1 2 3 "this is a vector"]
{:key "value" :two 2 }
#{"I" "am" "set" }
(println "this is a function call")
fn call


(println "this is a function call")
fn call         arguments


(println "this is a function call")
fn call          arguments


(println "this is a function call")


             list
syntax

(fn-name arg1 arg2 arg3 ...)
Functional Programming
Functional Programming
     A pure Function




    A impure Function
Functional Programming
             A pure Function
                                 don’t
                                 ...look outside their box
 Arguments        (Perform       ...modify anything, anywhere
                                  ...print messages to the user
             calculations, may   ...write to disk


Return Value call other pure     i.e. No Side Effects

                 functions)      E.g. f(x) = x * x + 1




            A impure Function
Functional Programming
             A pure Function
                                 don’t
                                 ...look outside their box
 Arguments        (Perform       ...modify anything, anywhere
                                  ...print messages to the user
             calculations, may   ...write to disk


Return Value call other pure     i.e. No Side Effects

                 functions)      E.g. f(x) = x * x + 1




           A impure Function
 Arguments                 Read External State
               (perform
Return Value calculations) Write External State
                             (Side Effect)
Functional
Programming
Functional
           Programming
• Project Euler (http://projecteuler.net)
Functional
          Programming
• Project Euler (http://projecteuler.net)
• Problem 1: If we list all the natural numbers
  below 10 that are multiples of 3 and 5, we
  get 3,5,6,9. The sum of these mutiples is 23.
Functional
          Programming
• Project Euler (http://projecteuler.net)
• Problem 1: If we list all the natural numbers
  below 10 that are multiples of 3 and 5, we
  get 3,5,6,9. The sum of these mutiples is 23.
  Find the sum of all the multiples of 3 and 5
  below 1000.
Project Euler: range
Project Euler: range

• I need a function to get a list of numbers
Project Euler: range

• I need a function to get a list of numbers
  (range 5) => (0 1 2 3 4)
Project Euler: range

• I need a function to get a list of numbers
  (range 5) => (0 1 2 3 4)
• So to get a range of (0-999) we can do
Project Euler: range

• I need a function to get a list of numbers
  (range 5) => (0 1 2 3 4)
• So to get a range of (0-999) we can do
  (range 1000)
Project Euler:numbers
 that are divisible by
       3 and 5

• zero?
• rem
Project Euler: filter

• Next something that returns a list of
  selected values from a larger list
  (filter even? (range 5)) => (0 2 4)
• filter function takes a predicate and a
  sequence (aka collection)
Project Euler: reduce
• Finally something to add up these numbers
• reduce takes a function and a sequence.
  Applies the function to the first two
  elements of the sequence, then applies the
  function to the result and third item and so
  on
  (reduce + (range 6)) ==> 15
Project Euler:Completed
          program
(defn div_by_3_or_5? [n]
      (or (zero? (rem n 3))
          (zero? (rem n 5))
         )
 )
(reduce +
         (filter div_by_3_or_5?
            (range 1000)
          )
   )
Books / Resources
http://alexott.net/en/clojure/video.html
Thank you
Appendix
Example of Clojure Code
10 threads manipulating one shared data structure, which consists of 100 vectors each one
containing 10 (initially sequential) unique numbers. Each thread then repeatedly selects two
random positions in two random vectors and swaps them. All changes to the vectors occur
in transactions by making use of clojure's software transactional memory system. That's
why even after 100 000 iterations of each thread no number got lost.
 (defn run [nvecs nitems nthreads niters]
   (let [vec-refs (vec (map (comp ref vec)
                             (partition nitems (range (* nvecs nitems)))))
         swap #(let [v1 (rand-int nvecs)
                      v2 (rand-int nvecs)
                      i1 (rand-int nitems)
                      i2 (rand-int nitems)]
                 (dosync
                  (let [temp (nth @(vec-refs v1) i1)]
                     (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2))
                     (alter (vec-refs v2) assoc i2 temp))))
         report #(do
                  (prn (map deref vec-refs))
                  (println "Distinct:"
                            (count (distinct (apply concat (map deref vec-refs))))))]
     (report)
     (dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap)))))
     (report)))

 (run 100 10 10 100000)

More Related Content

What's hot (20)

PDF
Kotlin, why?
Paweł Byszewski
 
PPTX
20170317 functional programming in julia
岳華 杜
 
PPTX
Yin Yangs of Software Development
Naveenkumar Muguda
 
PDF
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
GeeksLab Odessa
 
KEY
Clojure Intro
thnetos
 
PPTX
Understanding Javascript Engines
Parashuram N
 
PDF
Pune Clojure Course Outline
Baishampayan Ghose
 
PDF
MP in Clojure
Kent Ohashi
 
PDF
Kotlin Bytecode Generation and Runtime Performance
intelliyole
 
PDF
[Codemotion 2015] patrones de diseño con java8
Alonso Torres
 
PDF
C# for Java Developers
Jussi Pohjolainen
 
PDF
C# 7.x What's new and what's coming with C# 8
Christian Nagel
 
PDF
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
PDF
From Lisp to Clojure/Incanter and RAn Introduction
elliando dias
 
PDF
Orthogonal Functional Architecture
John De Goes
 
PPTX
Introduction to Julia Language
Diego Marinho de Oliveira
 
PDF
Introduction to Functional Programming
Francesco Bruni
 
PDF
All Aboard The Scala-to-PureScript Express!
John De Goes
 
PPTX
Introduction to Kotlin
Oswald Campesato
 
PDF
Why Haskell
Susan Potter
 
Kotlin, why?
Paweł Byszewski
 
20170317 functional programming in julia
岳華 杜
 
Yin Yangs of Software Development
Naveenkumar Muguda
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
GeeksLab Odessa
 
Clojure Intro
thnetos
 
Understanding Javascript Engines
Parashuram N
 
Pune Clojure Course Outline
Baishampayan Ghose
 
MP in Clojure
Kent Ohashi
 
Kotlin Bytecode Generation and Runtime Performance
intelliyole
 
[Codemotion 2015] patrones de diseño con java8
Alonso Torres
 
C# for Java Developers
Jussi Pohjolainen
 
C# 7.x What's new and what's coming with C# 8
Christian Nagel
 
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
From Lisp to Clojure/Incanter and RAn Introduction
elliando dias
 
Orthogonal Functional Architecture
John De Goes
 
Introduction to Julia Language
Diego Marinho de Oliveira
 
Introduction to Functional Programming
Francesco Bruni
 
All Aboard The Scala-to-PureScript Express!
John De Goes
 
Introduction to Kotlin
Oswald Campesato
 
Why Haskell
Susan Potter
 

Similar to Clojure intro (20)

PDF
Exploring Clojurescript
Luke Donnet
 
PPT
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
PPT
Functional Programming Past Present Future
IndicThreads
 
PDF
Introduction to Elixir
Diacode
 
PDF
Clojure
Rohit Vaidya
 
ODP
Getting started with Clojure
John Stevenson
 
PPTX
A brief introduction to lisp language
David Gu
 
PDF
Exploiting Concurrency with Dynamic Languages
Tobias Lindaaker
 
KEY
Scala clojure techday_2011
Thadeu Russo
 
PPTX
Fuel Up JavaScript with Functional Programming
Shine Xavier
 
PPTX
1P13 Python Review Session Covering various Topics
hussainmuhd1119
 
PPTX
Should i Go there
Shimi Bandiel
 
PDF
Clojure made-simple - John Stevenson
JAX London
 
PPTX
Concurrency Constructs Overview
stasimus
 
PDF
Booting into functional programming
Dhaval Dalal
 
PPT
Part 3-functions
ankita44
 
PDF
Hadoop + Clojure
elliando dias
 
PDF
Gnu octave
Milad Nourizade
 
PDF
Hw09 Hadoop + Clojure
Cloudera, Inc.
 
Exploring Clojurescript
Luke Donnet
 
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
Functional Programming Past Present Future
IndicThreads
 
Introduction to Elixir
Diacode
 
Clojure
Rohit Vaidya
 
Getting started with Clojure
John Stevenson
 
A brief introduction to lisp language
David Gu
 
Exploiting Concurrency with Dynamic Languages
Tobias Lindaaker
 
Scala clojure techday_2011
Thadeu Russo
 
Fuel Up JavaScript with Functional Programming
Shine Xavier
 
1P13 Python Review Session Covering various Topics
hussainmuhd1119
 
Should i Go there
Shimi Bandiel
 
Clojure made-simple - John Stevenson
JAX London
 
Concurrency Constructs Overview
stasimus
 
Booting into functional programming
Dhaval Dalal
 
Part 3-functions
ankita44
 
Hadoop + Clojure
elliando dias
 
Gnu octave
Milad Nourizade
 
Hw09 Hadoop + Clojure
Cloudera, Inc.
 
Ad

Recently uploaded (20)

PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Digital Circuits, important subject in CS
contactparinay1
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Ad

Clojure intro

  • 2. Agenda • Background • What is Clojure? • Why Clojure? • Some code..
  • 4. Clojure Vocabulary Functional LISP STM Programming lambda calculus recursion (λ) prefix notation JVM Lazy Sequences macros homoiconicity REPL S-expressions
  • 7. Programming Taxonomy http://en.wikipedia.org/wiki/ Comparison_of_programming_paradigms
  • 8. What is Clojure? Clojure is a dynamic, strongly typed, functional, high performance implementation of Lisp on the JVM (and CLR and yes JavaScript)
  • 9. What is Lisp? LISP stands for LISt Processing. Designed by John McCarthy. Implementation of lambda calculus. Oldest Language - Circa 1958 Uses Lots of ( )s - Polish Prefix notation Known for AI, DSLs etc
  • 11. What is lambda calculus? • Formal system computation by for expressing in Computer Science functions • Consists of λ expressions • variables v1, v2, ..., vn, ... • the abstraction symbols λ and . • parentheses ( ) • Examples • λx.x — is a function taking an argument x, and returning x Alonzo Church • f x — is a function f applied to an argument x
  • 12. Clojure History • Project led by Rich Hickey • 1.0 in 2007 • 1.1 in 2009 • 1.2 in 2010 • 1.3 in 2011 • 1.4 in 2012 • 1.5 released in March 2013 • Maintained on Github • contains one jar file clojure.jar • You can get it from Maven repo
  • 15. Q: How long have you been using Clojure?
  • 16. Q: What language did you use just prior to adopting Clojure ?
  • 17. Q: How would you characterize your use of Clojure today?
  • 18. Q: In which domain(s) are you using Clojure?
  • 19. Q: What have been the biggest wins for you in using Clojure?
  • 20. Q: Which development environment(s) do you use to work with Clojure?
  • 21. Why Clojure? • Good at working with Data • Rich literal types like arrays, sets, data types • Good at expressing algorithms • Functional • Great for concurrency • Immutable data structure • Innovative approach to state - STM
  • 25. Data literals (1 2 3 "this" "is" :a :list) [1 2 3 "this is a vector"] {:key "value" :two 2 } #{"I" "am" "set" }
  • 26. (println "this is a function call")
  • 27. fn call (println "this is a function call")
  • 28. fn call arguments (println "this is a function call")
  • 29. fn call arguments (println "this is a function call") list
  • 32. Functional Programming A pure Function A impure Function
  • 33. Functional Programming A pure Function don’t ...look outside their box Arguments (Perform ...modify anything, anywhere ...print messages to the user calculations, may ...write to disk Return Value call other pure i.e. No Side Effects functions) E.g. f(x) = x * x + 1 A impure Function
  • 34. Functional Programming A pure Function don’t ...look outside their box Arguments (Perform ...modify anything, anywhere ...print messages to the user calculations, may ...write to disk Return Value call other pure i.e. No Side Effects functions) E.g. f(x) = x * x + 1 A impure Function Arguments Read External State (perform Return Value calculations) Write External State (Side Effect)
  • 36. Functional Programming • Project Euler (http://projecteuler.net)
  • 37. Functional Programming • Project Euler (http://projecteuler.net) • Problem 1: If we list all the natural numbers below 10 that are multiples of 3 and 5, we get 3,5,6,9. The sum of these mutiples is 23.
  • 38. Functional Programming • Project Euler (http://projecteuler.net) • Problem 1: If we list all the natural numbers below 10 that are multiples of 3 and 5, we get 3,5,6,9. The sum of these mutiples is 23. Find the sum of all the multiples of 3 and 5 below 1000.
  • 40. Project Euler: range • I need a function to get a list of numbers
  • 41. Project Euler: range • I need a function to get a list of numbers (range 5) => (0 1 2 3 4)
  • 42. Project Euler: range • I need a function to get a list of numbers (range 5) => (0 1 2 3 4) • So to get a range of (0-999) we can do
  • 43. Project Euler: range • I need a function to get a list of numbers (range 5) => (0 1 2 3 4) • So to get a range of (0-999) we can do (range 1000)
  • 44. Project Euler:numbers that are divisible by 3 and 5 • zero? • rem
  • 45. Project Euler: filter • Next something that returns a list of selected values from a larger list (filter even? (range 5)) => (0 2 4) • filter function takes a predicate and a sequence (aka collection)
  • 46. Project Euler: reduce • Finally something to add up these numbers • reduce takes a function and a sequence. Applies the function to the first two elements of the sequence, then applies the function to the result and third item and so on (reduce + (range 6)) ==> 15
  • 47. Project Euler:Completed program (defn div_by_3_or_5? [n] (or (zero? (rem n 3)) (zero? (rem n 5)) ) ) (reduce + (filter div_by_3_or_5? (range 1000) ) )
  • 51. Example of Clojure Code 10 threads manipulating one shared data structure, which consists of 100 vectors each one containing 10 (initially sequential) unique numbers. Each thread then repeatedly selects two random positions in two random vectors and swaps them. All changes to the vectors occur in transactions by making use of clojure's software transactional memory system. That's why even after 100 000 iterations of each thread no number got lost. (defn run [nvecs nitems nthreads niters] (let [vec-refs (vec (map (comp ref vec) (partition nitems (range (* nvecs nitems))))) swap #(let [v1 (rand-int nvecs) v2 (rand-int nvecs) i1 (rand-int nitems) i2 (rand-int nitems)] (dosync (let [temp (nth @(vec-refs v1) i1)] (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2)) (alter (vec-refs v2) assoc i2 temp)))) report #(do (prn (map deref vec-refs)) (println "Distinct:" (count (distinct (apply concat (map deref vec-refs))))))] (report) (dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap))))) (report))) (run 100 10 10 100000)