SlideShare uma empresa Scribd logo
1 de 46
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

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 5Akshay 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 ParadigmsMiles Sabin
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypseelliando 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 ScalaDerek Chen-Becker
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigoujaxconf
 
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 ScalaFilip Krikava
 
Java 8 selected updates
Java 8 selected updatesJava 8 selected updates
Java 8 selected updatesVinay 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 TwitterAlex 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 ScalaRahul 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 StreamsEmiel Paasschens
 

Mais procurados (18)

55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
 
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
 
The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論
 
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
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
Core java
Core javaCore java
Core java
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypse
 
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
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
 
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
 
Java 8 selected updates
Java 8 selected updatesJava 8 selected updates
Java 8 selected updates
 
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
 
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...
 
Core java
Core javaCore java
Core java
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
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
 

Destaque

An Introduction To Java Profiling
An Introduction To Java ProfilingAn Introduction To Java Profiling
An Introduction To Java Profilingschlebu
 
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 ControlLeon Chen
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - StockholmJan Kronquist
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java SlidesVinit Vyas
 

Destaque (8)

An Introduction To Java Profiling
An Introduction To Java ProfilingAn Introduction To Java Profiling
An Introduction To Java Profiling
 
Introduction to JAVA
Introduction to JAVAIntroduction to JAVA
Introduction to JAVA
 
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
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
 
Core java slides
Core java slidesCore java slides
Core java slides
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java Slides
 

Semelhante a Clojure Fundamentals for Java Programmers

Typesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and PlayTypesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and PlayLuka Zakrajšek
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryPray Desai
 
oAW Presentation
oAW PresentationoAW Presentation
oAW PresentationSaif Kamaal
 
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!scalaconfjp
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with EpsilonSina Madani
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdprat0ham
 
What's a macro?: Learning by Examples
What's a macro?: Learning by ExamplesWhat's a macro?: Learning by Examples
What's a macro?: Learning by Exampleschibochibo
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, androidi i
 
U-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for DevelopersU-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for DevelopersMichael Rys
 
Presenter manual embedded systems (specially for summer interns)
Presenter manual   embedded systems (specially for summer interns)Presenter manual   embedded systems (specially for summer interns)
Presenter manual embedded systems (specially for summer interns)XPERT INFOTECH
 
Java New Evolution
Java New EvolutionJava New Evolution
Java New EvolutionAllan Huang
 
Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011Agora Group
 
Jslab rssh: JS as language platform
Jslab rssh:  JS as language platformJslab rssh:  JS as language platform
Jslab rssh: JS as language platformRuslan Shevchenko
 

Semelhante a Clojure Fundamentals for Java Programmers (20)

Typesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and PlayTypesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and Play
 
Java ce241
Java ce241Java ce241
Java ce241
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
oAW Presentation
oAW PresentationoAW Presentation
oAW Presentation
 
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
 
Scala-Ls1
Scala-Ls1Scala-Ls1
Scala-Ls1
 
What's a macro?: Learning by Examples
What's a macro?: Learning by ExamplesWhat's a macro?: Learning by Examples
What's a macro?: Learning by Examples
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
 
Java
Java Java
Java
 
U-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for DevelopersU-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for Developers
 
Presenter manual embedded systems (specially for summer interns)
Presenter manual   embedded systems (specially for summer interns)Presenter manual   embedded systems (specially for summer interns)
Presenter manual embedded systems (specially for summer interns)
 
The CoFX Data Model
The CoFX Data ModelThe CoFX Data Model
The CoFX Data Model
 
Java New Evolution
Java New EvolutionJava New Evolution
Java New Evolution
 
Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011
 
Jslab rssh: JS as language platform
Jslab rssh:  JS as language platformJslab rssh:  JS as language platform
Jslab rssh: JS as language platform
 

Mais de elliando dias

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slideselliando 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 ClojureScriptelliando dias
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structureselliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de containerelliando 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 Agilityelliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando 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 Webelliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduinoelliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorceryelliando 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 Designelliando dias
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makeselliando 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 Facebookelliando 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 Studyelliando dias
 

Mais de elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
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
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
 
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
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
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!
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
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
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
 
Rango
RangoRango
Rango
 
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
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
 
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
 

Último

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Último (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

Clojure Fundamentals 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