Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ClojureScript (FrOSCon 2012)

ClojureScript (FrOSCon 2012)

Clojure is a modern LISP running on the JVM. Persistent data structures, powerful macros, simple multithreading semantics and the read-eval-print-loop make programming easy and productive.

ClojureScripts opens the doors to a new versatile platform: The browser.

This presentation gives an overview over the ClojureScript project: IDEs, Tools, Libraries, and advantages and disadvantages in comparison with pure Javascript.

Moritz Ulrich

August 26, 2012
Tweet

Other Decks in Programming

Transcript

  1. Outline 1 Introduction 2 Code Samples 3 Implementation 4 Tool

    Support 5 Future Plans 6 Conclusion Moritz Ulrich ClojureScript FrOSCon 2012 2 / 32
  2. Hello World! Clojure (println "Hello World!") ClojureScript (.log js/console "Hello

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

    "Hello World!") Moritz Ulrich ClojureScript FrOSCon 2012 4 / 32
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. Compilation 1 Reader 2 Macros 3 Analysis 4 Emission 5

    Closure Compiler Moritz Ulrich ClojureScript FrOSCon 2012 15 / 32
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. ClojureScript One Sample single-page application Browser connected REPL Well documented

    Great (but complex) starting point Moritz Ulrich ClojureScript FrOSCon 2012 21 / 32
  20. 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
  21. 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
  22. 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
  23. Good for Single Page applications with much logic Re-use of

    code written for the server Moritz Ulrich ClojureScript FrOSCon 2012 27 / 32
  24. Not so good for Small utility scripts (high file size)

    High performance code Moritz Ulrich ClojureScript FrOSCon 2012 28 / 32
  25. 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
  26. 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
  27. So long. . . . . . and thanks for

    all the fish Thank you! Moritz Ulrich ClojureScript FrOSCon 2012 31 / 32