Академический Документы
Профессиональный Документы
Культура Документы
• 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
• 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)
(partition 3 [1 2 3 4 5 6 7 8 9])
-> ((1 2 3) (4 5 6) (7 8 9))
(new java.util.Date)
Thu Jun 05 12:37:32 EDT 2008
;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?
• Clojure way:
• Automatic/enforced
• No locks!
Direct references to
Mutable Objects
?
?
42
?
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
• 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