Introducing Clojure

From WikiEducator
Jump to: navigation, search


Introducing Clojure For Java Programmers
Convenor: Ben Evans
Participants:
Summary:

Language Features

If we could write a new language what would we take from the existing languages we already know

  1. JVM
  2. Compiled
  3. Runtime (thin)
  4. Dynamic type system
  5. Fresh approach to concurrency
  6. Compile time error checking
  7. Clean syntax


<sidebar> Rafi from Twitter - Scale out - presentation at oscon -- go watch They hired ppl to work on ruby gc and got nowhere and now hire full time Java GC dev. </sidebar>

AST is the syntax that we start with when compiling something. Therefore the transformation language should be as close to the AST This is akin to XSLT being written in XML

Shipping clojure

  • ship the source, not the compiled code
  • it compiles so fast it doesn't need interpretation and compile is quick because it is close the AST

Thread Safety

Clojure defs work with bindings - are always local to a thread. So if I change the binding, the other thread won't see it. The fundamental data type here is a clojure.lang.Var - think of it as a Clojure scalar skin over a Java data value.

STMS shared transaction memory system: Many threads can share state. This uses a clojure.lang.Ref (as one level of indirection over the value). One thread alters a reference and tries to commit

It will either succeed or roll back and fail. For this reason, the Clojure memory transactions cannot contain code with side-effects (although this can't be enforced by the runtime at present). If side-effects are present, they can occur an undetermined number of times.

For async thread comms Clojure has agents (ala Erlang/Scala)

Simplicity


code sample 

JAVA

                   fn1().fn2().fn3(); 
                  helloworld();

CLOSURE

                   (fn3 (fn2 (fn2))) 
                  (helloworld)

The closure first example shows how the method execution will happen, which isn't as clear from the Java one.


pointer datatype called var clojure.lang.Var

Var points to an Object



TODO: Add Ben's slide's to this page as they went in more detail!

Recommendations:

(as above)