SlideShare a Scribd company logo
Clojure
A Dynamic Programming Language for the JVM

 An Introduction for Java Programmers
                 Rich Hickey
Introduction
• Who are you?
 • Know/use Lisp?
 • Java/C#/Scala?
 • ML/Haskell?
 • Python, Ruby, Groovy?
 • Clojure?
• Any multithreaded programming?
Agenda
• Fundamentals
• Syntax and evaluation model
• Sequences
• Java Integration
• Concurrency
• Q&A
Clojure Fundamentals
• Dynamic
 • a new Lisp, not Common Lisp or Scheme
• Functional
 • emphasis on immutability
• Hosted on the JVM
• Supporting Concurrency
• Open Source
Why use a dynamic
       language?
• Flexibility
• Interactivity
• Concision
• Exploration
• Focus on your problem
Which dynamic language?
•   Many options on the JVM

    •   allow you to leverage your existing
        knowledge and code

•   Ports to JVM

    •   JRuby

    •   Jython

•   Native to JVM

    •   Groovy

    •   Clojure
Why Clojure?
• Expressive, elegant
• Good performance
 • Useful for the same tasks Java is
 • Wrapper-free Java access
• Powerful extensibility
• Functional programming and concurrency
Clojure is a Lisp
• Dynamic
• Code as data
• Reader
• Small core
• Sequences
• Syntactic abstraction
Dynamic development
• REPL - Read-eval-print-loop
• Define functions on the fly
• Load and compile code at runtime
• Introspection
• Interactive environment
Atomic Data Types
• Arbitrary precision integers -12345678987654


• Doubles , BigDecimals
           1.234              1.234M


• Ratios -22/7


• Strings -“fred”, Characters -a b c


• Symbols - fred ethel , Keywords -   :fred :ethel


• Booleans - true false  , Null -
                               nil


• Regex patterns    #“a*b”
Data Structures
• Lists - singly linked, grow at front
  •   (1 2 3 4 5), (fred ethel lucy), (list 1 2 3)


• Vectors - indexed access, grow at end
  •   [1 2 3 4 5], [fred ethel lucy]


• Maps - key/value associations
  •   {:a 1, :b 2, :c 3}, {1 “ethel” 2 “fred”}


• Sets  #{fred ethel lucy}


• Everything Nests
Syntax
• You’ve just seen it
• Data structures are the code
 • Homoiconicity
• No more text-based syntax
• Actually, syntax is in the interpretation of
  data structures
Traditional evaluation
Code
Text
       characters
                                                                          Effect

                    Compiler


                               bytecode


                                          Executable
                                                                    JVM
                                           .class/.jar




                                                         Run java
Clojure Evaluation
Code
Text
       characters
                                                                             Effect

                    Reader


                             data structures


                                               evaluator/
                                                            bytecode   JVM
                                                compiler
Interactivity
Code
Text
       characters
                                                                             Effect

                    Reader


                             data structures
       characters

                                               evaluator/
                                                            bytecode   JVM
                                                compiler



You
Programs writing Programs
 Code
 Text
        characters
                                                                                 Effect

                       Reader


                                 data structures
        characters

                                                   evaluator/
                                                                bytecode   JVM
                                                    compiler

                                data structures
 You
                     Program
Syntactic Abstraction
Code
Text
       characters
                                                                                    Effect

                      Reader


                                data structures
       characters

                                                   evaluator/
                                                                   bytecode   JVM
                                                    compiler

                               data structures
You
                    Program                      data structures




                                                    Program
                                                    (macro)
Expressions
• Everything is an expression
• All data literals represent themselves
 • Except:
   • Symbols
      • looks for binding to value, locally,
         then globally
    • Lists
     • An operation form
Operation forms

•   (op ...)

• op can be either:
 • one of very few special ops
 • macro
 • expression which yields a function
Special ops
•   Can have non-normal evaluation of arguments
    •   (def name value-expr)

        •   establishes a global variable
    •   (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
Macros
• Supplied with Clojure, and defined by user
• Argument forms are passed as data to the
    macro function, which returns a new data
    structure as a replacement for the macro call
•   (or x y)
               (let [or__158 x]
• becomes:       (if or__158 or__158 y))
• Many things that are ‘built-in’ to other languages
    are just macros in Clojure
Functions
• First-class values
   (def five 5)
   (def sqr (fn [x] (* x x)))
   (sqr five)
   25
• Maps are functions of their keys
   (def m {:fred :ethel :ricky :lucy})
   (m :fred)
   :ethel
Syntax Summary
• Things that would be declarations, control
  structures, function calls, operators, are all
  just lists with op at front:
          Java                Clojure
  int i = 5;            (def i 5)
  if(x == 0)            (if (zero? x)
    return y;             y
  else                    z)
    return z;
  x* y * z;             (* x y z)
  foo(x, y, z);         (foo x y z)
  foo.bar(x);           (. foo bar x)
Sequences
• Abstraction of traditional Lisp lists
•   (seq coll)

    • if collection is non-empty, return seq
      object on it, else nil
•   (first seq)

    • returns the first element
•   (rest seq)

    • returns a seq of the rest of the elements,
      or nil if no more
Sequence Library
(drop 2 [1 2 3 4 5]) -> (3 4 5)

(take 9 (cycle [1 2 3 4]))
-> (1 2 3 4 1 2 3 4 1)

(interleave [:a :b :c :d :e] [1 2 3 4 5])
-> (:a 1 :b 2 :c 3 :d 4 :e 5)

(partition 3 [1 2 3 4 5 6 7 8 9])
-> ((1 2 3) (4 5 6) (7 8 9))

(map vector [:a :b :c :d :e] [1 2 3 4 5])
-> ([:a 1] [:b 2] [:c 3] [:d 4] [:e 5])

(apply str (interpose , "asdf"))
-> "a,s,d,f"

(reduce + (range 100)) -> 4950
Java Interop
(. Math PI)
3.141592653589793

(.. System getProperties (get "java.version"))
"1.5.0_13"

(new java.util.Date)
Thu Jun 05 12:37:32 EDT 2008

(doto (JFrame.) (add (JLabel. "Hello World")) pack show)

;expands to:
(let* [G__1837 (JFrame.)]
   (do (. G__1837 (add (JLabel. "Hello World")))
       (. G__1837 pack)
       (. G__1837 show))
   G__1837)
Java Integration
• Clojure strings are Java Strings, numbers are
  Numbers, collections implement Collection,
  fns implement Callable and Runnable etc.
• Core abstractions, like seq, are Java interfaces
• Clojure seq library works on Java Iterables,
  Strings and arrays.
• Implement and extend Java interfaces and
  classes
• New primitive arithmetic support equals
  Java’s speed.
Swing Example
(import '(javax.swing JFrame JLabel JTextField JButton)
        '(java.awt.event ActionListener) '(java.awt GridLayout))
(defn celsius []
  (let [frame (JFrame. "Celsius Converter")
        temp-text (JTextField.)
        celsius-label (JLabel. "Celsius")
        convert-button (JButton. "Convert")
        fahrenheit-label (JLabel. "Fahrenheit")]
    (.addActionListener convert-button
       (proxy [ActionListener] []
          (actionPerformed [evt]
             (let [c (. Double parseDouble (.getText temp-text))]
               (.setText fahrenheit-label
                  (str (+ 32 (* 1.8 c)) " Fahrenheit"))))))
    (doto frame
      (setLayout (GridLayout. 2 2 3 3))
      (add temp-text) (add celsius-label)
      (add convert-button) (add fahrenheit-label)
      (setSize 300 80) (setVisible true))))
(celsius)
Functional Programming
• Immutable data + first-class functions
• Functions produce same output given same
  input, and are free of side effects
• Could always be done by discipline/convention
• Pure functional languages tend to strongly static
  types (ML, Haskell)
  • Not for everyone, or every task
• Dynamic functional languages are rarer
 • Clojure, Erlang
Why Functional Programming?

 • Easier to reason about
 • Easier to test
 • Essential for concurrency (IMO)
  • Java Concurrency in Practice - Goetz
 • Additional benefits for purely functional
   languages (static analysis, proof, program
   transformation), but not Clojure
Which Functional Language?
• Fewer choices on the JVM
• CAL
 • Haskell-like, strong type system
• Scala
 • Type system, but immutability optional
• Clojure
 • Dynamic types, immutable data
Persistent Data Structures
• Immutable, + old version of the collection is
  still available after 'changes'
• Collection maintains its performance
  guarantees for most operations
  • Therefore new versions are not full copies
• All Clojure data structures persistent
  • Hash map and vector both based upon
    array mapped hash tries (Bagwell)
  • Sorted map is red-black tree
Structural Sharing
• Key to efficient ‘copies’ and therefore
  persistence
• Everything is final so no chance of
  interference
• Thread safe
• Iteration safe
Path Copying
                                 HashMap
HashMap
                                 int count    16
int count    15
                                 INode root
INode root
Concurrency
• Interleaved/simultaneous execution
• Must avoid seeing/yielding inconsistent data
• The more components there are to the data,
  the more difficult to keep consistent
• The more steps in a logical change, the more
  difficult to keep consistent
• Opportunities for automatic parallelism
 • Emphasis here on coordination
State - You’re doing it wrong
• Mutable objects are the new spaghetti code
 • Hard to understand, test, reason about
 • Concurrency disaster
 • Terrible default architecture (Java/C#/
    Python/Ruby/Groovy/CLOS...)
• Doing the right thing is very difficult
 • Languages matter!
Concurrency Methods
•   Conventional way:

    •   Direct references to mutable objects

    •   Lock and worry (manual/convention)

•   Clojure way:

    •   Indirect references to immutable persistent data
        structures

    •   Concurrency semantics for references

        •   Automatic/enforced

        •   No locks!
Direct references to
      Mutable Objects
                                 ?
                                 ?
                                42
                                 ?
                                 6




Ensuring a consistent Object is on your head
Indirect references to
  Immutable Objects
                          "fred"
                         "ethel"
                            42
                            17
                             6




  Never an inconsistent Object
Persistent ‘Edit’
               "fred"
              "ethel"
                 42
                 17
                  6




              "ricky"
              "lucy"
                 42
                 17
                  6
Atomic Update
           "fred"
          "ethel"
             42
             17
              6




          "ricky"
          "lucy"
             42
             17
              6
Clojure References
• The only things that mutate are references
  themselves, in a controlled way
• 3 types of mutable references
 • Vars - Isolate changes within threads
 • Refs - Share synchronous coordinated
    changes between threads
  • Agents - Share asynchronous
    independent changes between threads
Refs and Transactions
• Software transactional memory system (STM)
• Refs can only be changed within a transaction
• All changes are Atomic and Isolated
 • Every change to Refs made within a
    transaction occurs or none do
  • No transaction sees the effects of any
    other transaction while it is running
• Transactions are speculative
 • Will be retried automatically if conflict
 • Must avoid side-effects!
Concurrency Demo
•   Ant colony simulation

•   World populated with food and ants

•   Ants find food, bring home, drop pheromones

•   Sense pheromones, food, home

•   Ants act independently, on multiple real threads

•   Model pheromone evaporation

•   Animated GUI
•   < 250 lines of Clojure
And much more!
• Metadata
• Recursive functional looping
• Destructuring binding in let/fn/loop
• List comprehensions (for)
• Relational set algebra
• Multimethods
• Parallel computation
• Namespaces, zippers, XML ...
Thanks for listening!




      http://clojure.org

More Related Content

What's hot (18)

55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
Boulder Java User's Group
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
Akshay Nagpurkar
 
The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論
scalaconfjp
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
น้องน๊อต อยากเหยียบดวงจันทร์
 
An Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsAn Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional Paradigms
Miles Sabin
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
Stephen Colebourne
 
Core java
Core javaCore java
Core java
kasaragaddaslide
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypse
elliando dias
 
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaA Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
Jussi Pohjolainen
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
jaxconf
 
Model Manipulation Using Embedded DSLs in Scala
Model Manipulation Using Embedded DSLs in ScalaModel Manipulation Using Embedded DSLs in Scala
Model Manipulation Using Embedded DSLs in Scala
Filip Krikava
 
Java 8 selected updates
Java 8 selected updatesJava 8 selected updates
Java 8 selected updates
Vinay H G
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
Alex Payne
 
Mixing Source and Bytecode: A Case for Compilation By Normalization (OOPSLA 2...
Mixing Source and Bytecode: A Case for Compilation By Normalization (OOPSLA 2...Mixing Source and Bytecode: A Case for Compilation By Normalization (OOPSLA 2...
Mixing Source and Bytecode: A Case for Compilation By Normalization (OOPSLA 2...
lennartkats
 
Core java
Core javaCore java
Core java
Savita Rawat
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Rahul Jain
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
Akshay Nagpurkar
 
The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論
scalaconfjp
 
An Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsAn Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional Paradigms
Miles Sabin
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypse
elliando dias
 
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaA Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
jaxconf
 
Model Manipulation Using Embedded DSLs in Scala
Model Manipulation Using Embedded DSLs in ScalaModel Manipulation Using Embedded DSLs in Scala
Model Manipulation Using Embedded DSLs in Scala
Filip Krikava
 
Java 8 selected updates
Java 8 selected updatesJava 8 selected updates
Java 8 selected updates
Vinay H G
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
Alex Payne
 
Mixing Source and Bytecode: A Case for Compilation By Normalization (OOPSLA 2...
Mixing Source and Bytecode: A Case for Compilation By Normalization (OOPSLA 2...Mixing Source and Bytecode: A Case for Compilation By Normalization (OOPSLA 2...
Mixing Source and Bytecode: A Case for Compilation By Normalization (OOPSLA 2...
lennartkats
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Rahul Jain
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 

Viewers also liked (8)

An Introduction To Java Profiling
An Introduction To Java ProfilingAn Introduction To Java Profiling
An Introduction To Java Profiling
schlebu
 
Introduction to JAVA
Introduction to JAVAIntroduction to JAVA
Introduction to JAVA
javatrainingonline
 
Introduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlIntroduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission Control
Leon Chen
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
BG Java EE Course
 
Core java slides
Core java slidesCore java slides
Core java slides
Abhilash Nair
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
Jan Kronquist
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Veerabadra Badra
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java Slides
Vinit Vyas
 
An Introduction To Java Profiling
An Introduction To Java ProfilingAn Introduction To Java Profiling
An Introduction To Java Profiling
schlebu
 
Introduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlIntroduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission Control
Leon Chen
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
Jan Kronquist
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java Slides
Vinit Vyas
 
Ad

Similar to Clojure - An Introduction for Java Programmers (20)

Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
Bert Van Vreckem
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
Abbas Raza
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
futurespective
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
elliando dias
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
wpgreenway
 
Introduction to Clojure and why it's hot for Sart-Ups
Introduction to Clojure and why it's hot for Sart-UpsIntroduction to Clojure and why it's hot for Sart-Ups
Introduction to Clojure and why it's hot for Sart-Ups
edlich
 
Write Your Own JVM Compiler
Write Your Own JVM CompilerWrite Your Own JVM Compiler
Write Your Own JVM Compiler
Erin Dees
 
What is new and cool j2se & java
What is new and cool j2se & javaWhat is new and cool j2se & java
What is new and cool j2se & java
Eugene Bogaart
 
Clojure Interoperability
Clojure InteroperabilityClojure Interoperability
Clojure Interoperability
rik0
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
Yardena Meymann
 
Scala clojure techday_2011
Scala clojure techday_2011Scala clojure techday_2011
Scala clojure techday_2011
Thadeu Russo
 
Not Everything is an Object - Rocksolid Tour 2013
Not Everything is an Object  - Rocksolid Tour 2013Not Everything is an Object  - Rocksolid Tour 2013
Not Everything is an Object - Rocksolid Tour 2013
Gary Short
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
JAX London
 
I know Java, why should I consider Clojure?
I know Java, why should I consider Clojure?I know Java, why should I consider Clojure?
I know Java, why should I consider Clojure?
sbjug
 
Clojure Small Intro
Clojure Small IntroClojure Small Intro
Clojure Small Intro
John Vlachoyiannis
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
thnetos
 
Jvm Language Summit Rose 20081016
Jvm Language Summit Rose 20081016Jvm Language Summit Rose 20081016
Jvm Language Summit Rose 20081016
Eduardo Pelegri-Llopart
 
All about scala
All about scalaAll about scala
All about scala
Yardena Meymann
 
Java Starting
Java StartingJava Starting
Java Starting
Raja Sekhar
 
JDK7: Improved support for dynamic languages
JDK7: Improved support for dynamic languagesJDK7: Improved support for dynamic languages
JDK7: Improved support for dynamic languages
Sander Mak (@Sander_Mak)
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
Abbas Raza
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
elliando dias
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
wpgreenway
 
Introduction to Clojure and why it's hot for Sart-Ups
Introduction to Clojure and why it's hot for Sart-UpsIntroduction to Clojure and why it's hot for Sart-Ups
Introduction to Clojure and why it's hot for Sart-Ups
edlich
 
Write Your Own JVM Compiler
Write Your Own JVM CompilerWrite Your Own JVM Compiler
Write Your Own JVM Compiler
Erin Dees
 
What is new and cool j2se & java
What is new and cool j2se & javaWhat is new and cool j2se & java
What is new and cool j2se & java
Eugene Bogaart
 
Clojure Interoperability
Clojure InteroperabilityClojure Interoperability
Clojure Interoperability
rik0
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
Yardena Meymann
 
Scala clojure techday_2011
Scala clojure techday_2011Scala clojure techday_2011
Scala clojure techday_2011
Thadeu Russo
 
Not Everything is an Object - Rocksolid Tour 2013
Not Everything is an Object  - Rocksolid Tour 2013Not Everything is an Object  - Rocksolid Tour 2013
Not Everything is an Object - Rocksolid Tour 2013
Gary Short
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
JAX London
 
I know Java, why should I consider Clojure?
I know Java, why should I consider Clojure?I know Java, why should I consider Clojure?
I know Java, why should I consider Clojure?
sbjug
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
thnetos
 
JDK7: Improved support for dynamic languages
JDK7: Improved support for dynamic languagesJDK7: Improved support for dynamic languages
JDK7: Improved support for dynamic languages
Sander Mak (@Sander_Mak)
 
Ad

More from elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
elliando dias
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
elliando dias
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
elliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
elliando dias
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
elliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
elliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
Ragel talk
Ragel talkRagel talk
Ragel talk
elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
elliando dias
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
elliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
elliando dias
 
Rango
RangoRango
Rango
elliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
elliando dias
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
elliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
elliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 
Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
elliando dias
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
elliando dias
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
elliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
elliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
elliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
elliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
elliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
elliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
elliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 

Recently uploaded (20)

Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 

Clojure - An Introduction for Java Programmers

  • 1. Clojure A Dynamic Programming Language for the JVM An Introduction for Java Programmers Rich Hickey
  • 2. Introduction • Who are you? • Know/use Lisp? • Java/C#/Scala? • ML/Haskell? • Python, Ruby, Groovy? • Clojure? • Any multithreaded programming?
  • 3. Agenda • Fundamentals • Syntax and evaluation model • Sequences • Java Integration • Concurrency • Q&A
  • 4. Clojure Fundamentals • Dynamic • a new Lisp, not Common Lisp or Scheme • Functional • emphasis on immutability • Hosted on the JVM • Supporting Concurrency • Open Source
  • 5. Why use a dynamic language? • Flexibility • Interactivity • Concision • Exploration • Focus on your problem
  • 6. Which dynamic language? • Many options on the JVM • allow you to leverage your existing knowledge and code • Ports to JVM • JRuby • Jython • Native to JVM • Groovy • Clojure
  • 7. Why Clojure? • Expressive, elegant • Good performance • Useful for the same tasks Java is • Wrapper-free Java access • Powerful extensibility • Functional programming and concurrency
  • 8. Clojure is a Lisp • Dynamic • Code as data • Reader • Small core • Sequences • Syntactic abstraction
  • 9. Dynamic development • REPL - Read-eval-print-loop • Define functions on the fly • Load and compile code at runtime • Introspection • Interactive environment
  • 10. Atomic Data Types • Arbitrary precision integers -12345678987654 • Doubles , BigDecimals 1.234 1.234M • Ratios -22/7 • Strings -“fred”, Characters -a b c • Symbols - fred ethel , Keywords - :fred :ethel • Booleans - true false , Null - nil • Regex patterns #“a*b”
  • 11. Data Structures • Lists - singly linked, grow at front • (1 2 3 4 5), (fred ethel lucy), (list 1 2 3) • Vectors - indexed access, grow at end • [1 2 3 4 5], [fred ethel lucy] • Maps - key/value associations • {:a 1, :b 2, :c 3}, {1 “ethel” 2 “fred”} • Sets #{fred ethel lucy} • Everything Nests
  • 12. Syntax • You’ve just seen it • Data structures are the code • Homoiconicity • No more text-based syntax • Actually, syntax is in the interpretation of data structures
  • 13. Traditional evaluation Code Text characters Effect Compiler bytecode Executable JVM .class/.jar Run java
  • 14. Clojure Evaluation Code Text characters Effect Reader data structures evaluator/ bytecode JVM compiler
  • 15. Interactivity Code Text characters Effect Reader data structures characters evaluator/ bytecode JVM compiler You
  • 16. Programs writing Programs Code Text characters Effect Reader data structures characters evaluator/ bytecode JVM compiler data structures You Program
  • 17. Syntactic Abstraction Code Text characters Effect Reader data structures characters evaluator/ bytecode JVM compiler data structures You Program data structures Program (macro)
  • 18. Expressions • Everything is an expression • All data literals represent themselves • Except: • Symbols • looks for binding to value, locally, then globally • Lists • An operation form
  • 19. Operation forms • (op ...) • op can be either: • one of very few special ops • macro • expression which yields a function
  • 20. Special ops • Can have non-normal evaluation of arguments • (def name value-expr) • establishes a global variable • (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
  • 21. Macros • Supplied with Clojure, and defined by user • Argument forms are passed as data to the macro function, which returns a new data structure as a replacement for the macro call • (or x y) (let [or__158 x] • becomes: (if or__158 or__158 y)) • Many things that are ‘built-in’ to other languages are just macros in Clojure
  • 22. Functions • First-class values (def five 5) (def sqr (fn [x] (* x x))) (sqr five) 25 • Maps are functions of their keys (def m {:fred :ethel :ricky :lucy}) (m :fred) :ethel
  • 23. Syntax Summary • Things that would be declarations, control structures, function calls, operators, are all just lists with op at front: Java Clojure int i = 5; (def i 5) if(x == 0) (if (zero? x) return y; y else z) return z; x* y * z; (* x y z) foo(x, y, z); (foo x y z) foo.bar(x); (. foo bar x)
  • 24. Sequences • Abstraction of traditional Lisp lists • (seq coll) • if collection is non-empty, return seq object on it, else nil • (first seq) • returns the first element • (rest seq) • returns a seq of the rest of the elements, or nil if no more
  • 25. Sequence Library (drop 2 [1 2 3 4 5]) -> (3 4 5) (take 9 (cycle [1 2 3 4])) -> (1 2 3 4 1 2 3 4 1) (interleave [:a :b :c :d :e] [1 2 3 4 5]) -> (:a 1 :b 2 :c 3 :d 4 :e 5) (partition 3 [1 2 3 4 5 6 7 8 9]) -> ((1 2 3) (4 5 6) (7 8 9)) (map vector [:a :b :c :d :e] [1 2 3 4 5]) -> ([:a 1] [:b 2] [:c 3] [:d 4] [:e 5]) (apply str (interpose , "asdf")) -> "a,s,d,f" (reduce + (range 100)) -> 4950
  • 26. Java Interop (. Math PI) 3.141592653589793 (.. System getProperties (get "java.version")) "1.5.0_13" (new java.util.Date) Thu Jun 05 12:37:32 EDT 2008 (doto (JFrame.) (add (JLabel. "Hello World")) pack show) ;expands to: (let* [G__1837 (JFrame.)] (do (. G__1837 (add (JLabel. "Hello World"))) (. G__1837 pack) (. G__1837 show)) G__1837)
  • 27. Java Integration • Clojure strings are Java Strings, numbers are Numbers, collections implement Collection, fns implement Callable and Runnable etc. • Core abstractions, like seq, are Java interfaces • Clojure seq library works on Java Iterables, Strings and arrays. • Implement and extend Java interfaces and classes • New primitive arithmetic support equals Java’s speed.
  • 28. Swing Example (import '(javax.swing JFrame JLabel JTextField JButton) '(java.awt.event ActionListener) '(java.awt GridLayout)) (defn celsius [] (let [frame (JFrame. "Celsius Converter") temp-text (JTextField.) celsius-label (JLabel. "Celsius") convert-button (JButton. "Convert") fahrenheit-label (JLabel. "Fahrenheit")] (.addActionListener convert-button (proxy [ActionListener] [] (actionPerformed [evt] (let [c (. Double parseDouble (.getText temp-text))] (.setText fahrenheit-label (str (+ 32 (* 1.8 c)) " Fahrenheit")))))) (doto frame (setLayout (GridLayout. 2 2 3 3)) (add temp-text) (add celsius-label) (add convert-button) (add fahrenheit-label) (setSize 300 80) (setVisible true)))) (celsius)
  • 29. Functional Programming • Immutable data + first-class functions • Functions produce same output given same input, and are free of side effects • Could always be done by discipline/convention • Pure functional languages tend to strongly static types (ML, Haskell) • Not for everyone, or every task • Dynamic functional languages are rarer • Clojure, Erlang
  • 30. Why Functional Programming? • Easier to reason about • Easier to test • Essential for concurrency (IMO) • Java Concurrency in Practice - Goetz • Additional benefits for purely functional languages (static analysis, proof, program transformation), but not Clojure
  • 31. Which Functional Language? • Fewer choices on the JVM • CAL • Haskell-like, strong type system • Scala • Type system, but immutability optional • Clojure • Dynamic types, immutable data
  • 32. Persistent Data Structures • Immutable, + old version of the collection is still available after 'changes' • Collection maintains its performance guarantees for most operations • Therefore new versions are not full copies • All Clojure data structures persistent • Hash map and vector both based upon array mapped hash tries (Bagwell) • Sorted map is red-black tree
  • 33. Structural Sharing • Key to efficient ‘copies’ and therefore persistence • Everything is final so no chance of interference • Thread safe • Iteration safe
  • 34. Path Copying HashMap HashMap int count 16 int count 15 INode root INode root
  • 35. Concurrency • Interleaved/simultaneous execution • Must avoid seeing/yielding inconsistent data • The more components there are to the data, the more difficult to keep consistent • The more steps in a logical change, the more difficult to keep consistent • Opportunities for automatic parallelism • Emphasis here on coordination
  • 36. State - You’re doing it wrong • Mutable objects are the new spaghetti code • Hard to understand, test, reason about • Concurrency disaster • Terrible default architecture (Java/C#/ Python/Ruby/Groovy/CLOS...) • Doing the right thing is very difficult • Languages matter!
  • 37. Concurrency Methods • Conventional way: • Direct references to mutable objects • Lock and worry (manual/convention) • Clojure way: • Indirect references to immutable persistent data structures • Concurrency semantics for references • Automatic/enforced • No locks!
  • 38. Direct references to Mutable Objects ? ? 42 ? 6 Ensuring a consistent Object is on your head
  • 39. Indirect references to Immutable Objects "fred" "ethel" 42 17 6 Never an inconsistent Object
  • 40. Persistent ‘Edit’ "fred" "ethel" 42 17 6 "ricky" "lucy" 42 17 6
  • 41. Atomic Update "fred" "ethel" 42 17 6 "ricky" "lucy" 42 17 6
  • 42. Clojure References • The only things that mutate are references themselves, in a controlled way • 3 types of mutable references • Vars - Isolate changes within threads • Refs - Share synchronous coordinated changes between threads • Agents - Share asynchronous independent changes between threads
  • 43. Refs and Transactions • Software transactional memory system (STM) • Refs can only be changed within a transaction • All changes are Atomic and Isolated • Every change to Refs made within a transaction occurs or none do • No transaction sees the effects of any other transaction while it is running • Transactions are speculative • Will be retried automatically if conflict • Must avoid side-effects!
  • 44. Concurrency Demo • Ant colony simulation • World populated with food and ants • Ants find food, bring home, drop pheromones • Sense pheromones, food, home • Ants act independently, on multiple real threads • Model pheromone evaporation • Animated GUI • < 250 lines of Clojure
  • 45. And much more! • Metadata • Recursive functional looping • Destructuring binding in let/fn/loop • List comprehensions (for) • Relational set algebra • Multimethods • Parallel computation • Namespaces, zippers, XML ...
  • 46. Thanks for listening! http://clojure.org