Slide 1

Slide 1 text

ClojureScript Moritz Ulrich FrOSCon 2012 Moritz Ulrich ClojureScript FrOSCon 2012 1 / 32

Slide 2

Slide 2 text

Outline 1 Introduction 2 Code Samples 3 Implementation 4 Tool Support 5 Future Plans 6 Conclusion Moritz Ulrich ClojureScript FrOSCon 2012 2 / 32

Slide 3

Slide 3 text

Hello World! Clojure (println "Hello World!") ClojureScript (.log js/console "Hello World!") Moritz Ulrich ClojureScript FrOSCon 2012 3 / 32

Slide 4

Slide 4 text

Hello World v2 With clojure.core/*print-fn* bound to a function: (println "Hello World!") Moritz Ulrich ClojureScript FrOSCon 2012 4 / 32

Slide 5

Slide 5 text

Clojure? Modern LISP Targets the JVM Great Java-Interop Persistent Data Structures Easy concurrent programming Built-in STM Reactive Actors (‘Agents’) Good encapsulation of State Moritz Ulrich ClojureScript FrOSCon 2012 5 / 32

Slide 6

Slide 6 text

ClojureScript? All advantages of Clojure Functional Programming Persistent Data Structures Powerful concise Syntax Great Host Interop Runs in the Browser Wide target range No eval Reader still available! Moritz Ulrich ClojureScript FrOSCon 2012 6 / 32

Slide 7

Slide 7 text

ClojureScript! Created by Rich Hickey in 2011 Clojure Compiler which targets Javascript Same Runtime as Clojure Data Structures clojure.core Code Sharing possible Cleaner Codebase 100% Clojure, no Java/Javascript Much of Clojure is implemented in Java Moritz Ulrich ClojureScript FrOSCon 2012 7 / 32

Slide 8

Slide 8 text

Proof? find . -name *.js ./samples/hello-js/externed-lib.js ./samples/hello-js/externs.js ./samples/hello-js/my-external-lib.js ./src/cljs/cljs/nodejs_externs.js cat ./src/cljs/cljs/nodejs_externs.js function require(){} function process(){} Moritz Ulrich ClojureScript FrOSCon 2012 8 / 32

Slide 9

Slide 9 text

Why target Javascript? Faster startup time Widespread platform (Browsers) Node.js Advantages when using same code Server- and Client-Side Moritz Ulrich ClojureScript FrOSCon 2012 9 / 32

Slide 10

Slide 10 text

Platform Interop Function calls (.log js/console "Foobar") (js/alert 42) Properties (.-location js/window) (set! (.-prop obj) "Foo") Moritz Ulrich ClojureScript FrOSCon 2012 10 / 32

Slide 11

Slide 11 text

Interop: Syntactic Sugar Double Dot (.. ($ "#my-table") (children "tr") (children "td") (hide)) Anonymous Functions (fn [a b] (+ a b)) #(+ %1 %2) Moritz Ulrich ClojureScript FrOSCon 2012 11 / 32

Slide 12

Slide 12 text

DOM Manipulation Google Closure jQuery Moritz Ulrich ClojureScript FrOSCon 2012 12 / 32

Slide 13

Slide 13 text

Google Closure Javascript Library by Google Advantages Used by ClojureScript itself Very rich library containing many kinds of UI elements Integrates nicely with the Google Closure compiler Disadvantages Hard to use Usually hard to integrate in legacy codebases As of May 2012: No way to set data-attributes Moritz Ulrich ClojureScript FrOSCon 2012 13 / 32

Slide 14

Slide 14 text

jQuery Advantages More concise to use Widely known Nice wrappers available (jayq) Disadvantages Syntax doesn’t integrate very good Uses own Array type which doesn’t work out-of-the-box with Clojure’s Sequence abstractions Moritz Ulrich ClojureScript FrOSCon 2012 14 / 32

Slide 15

Slide 15 text

Compilation 1 Reader 2 Macros 3 Analysis 4 Emission 5 Closure Compiler Moritz Ulrich ClojureScript FrOSCon 2012 15 / 32

Slide 16

Slide 16 text

Closure Compiler Optimizing compiler for Javascript Performs the following: Warnings Dead-code elimination Optimization Code must be written in a very strict style ClojureScript generates such code Moritz Ulrich ClojureScript FrOSCon 2012 16 / 32

Slide 17

Slide 17 text

Size of generated Code Code (ns foo.bar) (defn ^:export greet [name] (js/alert (str "Hello, " name "!"))) Result 91914 out-advanced.js 724380 out-pretty.js Moritz Ulrich ClojureScript FrOSCon 2012 17 / 32

Slide 18

Slide 18 text

Limitations No Multithreading Atoms and Refs are still useful Complicated use of macros Many Clojure libraries don’t work without modifications Moritz Ulrich ClojureScript FrOSCon 2012 18 / 32

Slide 19

Slide 19 text

Available Libraries User-Interface Google Closure jQuery UI DOM Manipulation/Generation jayq (jQuery) crate/hiccups (Hiccup) enfocus (Enlive) All other Javascript libraries Moritz Ulrich ClojureScript FrOSCon 2012 19 / 32

Slide 20

Slide 20 text

IDEs Editor Support clojurescript-mode (Emacs) Build Tools lein-cljsbuild (Leiningen) cljs-watch REPL lein cljsbuild repl-{listen,rhino} Moritz Ulrich ClojureScript FrOSCon 2012 20 / 32

Slide 21

Slide 21 text

ClojureScript One Sample single-page application Browser connected REPL Well documented Great (but complex) starting point Moritz Ulrich ClojureScript FrOSCon 2012 21 / 32

Slide 22

Slide 22 text

Future Plans Pluggable Backends Source Maps Reactive Programming Moritz Ulrich ClojureScript FrOSCon 2012 22 / 32

Slide 23

Slide 23 text

Recent: Pluggable Backends Summer of Code Project by Raphael Amiard Lexer extracted from monolithic Compiler Compiler implemented as modular Backend Soon: Lua Python C Malbolge? Moritz Ulrich ClojureScript FrOSCon 2012 23 / 32

Slide 24

Slide 24 text

Source Maps Map from compiled Javascript to ClojureScript Great for debugging errors Implemented in Chrome, support in ClojureScript coming Moritz Ulrich ClojureScript FrOSCon 2012 24 / 32

Slide 25

Slide 25 text

Reactive Programming Most stuff happens in the DOM Manual DOM Manipulation is cumbersome Solution: Bind values of elements/data-structures to modified values of other data structures Moritz Ulrich ClojureScript FrOSCon 2012 25 / 32

Slide 26

Slide 26 text

Conclusions Moritz Ulrich ClojureScript FrOSCon 2012 26 / 32

Slide 27

Slide 27 text

Good for Single Page applications with much logic Re-use of code written for the server Moritz Ulrich ClojureScript FrOSCon 2012 27 / 32

Slide 28

Slide 28 text

Not so good for Small utility scripts (high file size) High performance code Moritz Ulrich ClojureScript FrOSCon 2012 28 / 32

Slide 29

Slide 29 text

Starting Points Clojure Google Group http://groups.google.com/group/clojure ClojureScript on Github https://github.com/clojure/clojurescript (Don’t follow the ‘Quick Start’ Guide! Use lein-cljsbuild for building projects or starting a REPL.) lein-cljsbuild https://github.com/emezeske/lein-cljsbuild ClojureScript One http://clojurescriptone.com/ Moritz Ulrich ClojureScript FrOSCon 2012 29 / 32

Slide 30

Slide 30 text

Links Google Closure https://developers.google.com/closure/ jQuery http://jquery.com/ jayq https://github.com/ibdknox/jayq crate https://github.com/ibdknox/crate hiccups https://github.com/teropa/hiccups enfocus https://github.com/ckirkendall/enfocus cljs-watch https://github.com/ibdknox/cljs-watch Moritz Ulrich ClojureScript FrOSCon 2012 30 / 32

Slide 31

Slide 31 text

So long. . . . . . and thanks for all the fish Thank you! Moritz Ulrich ClojureScript FrOSCon 2012 31 / 32

Slide 32

Slide 32 text

Contact Moritz Ulrich [email protected] https://github.com/the-kenny/ http://twitter.com/thek enny Moritz Ulrich ClojureScript FrOSCon 2012 32 / 32