O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ClojureScript: Up and Running

Book Description

Learn how to build complete client-side applications with ClojureScript, the Clojure language variant that compiles to optimized JavaScript. This hands-on introduction shows you how ClojureScript not only has similarities to JavaScript—without the flaws—but also supports the full semantics of its parent language. You’ll delve into ClojureScript’s immutable data structures, lazy sequences, first-class functions, macros, and support for JavaScript libraries.

No previous experience with Clojure or ClojureScript is necessary. If you’re familiar with JavaScript, HTML, CSS, and the DOM, you’ll quickly discover that ClojureScript has the same reach as JavaScript, but with more power.

  • Start writing ClojureScript code with the Leiningen build system
  • Learn how the ClojureScript compiler works to produce optimized JavaScript
  • Use JavaScript functions and libraries directly from ClojureScript code
  • Explore functions in Clojure’s sequence library such as map, reduce, and filter
  • Use macros to define new control structures or embed domain-specific languages
  • Compile manually or script your own workflow with ClojureScript’s compiler tools
  • Integrate ClojureScript with Clojure on the JVM to build powerful client-server applications

Table of Contents

  1. Preface
    1. Who Should Read This Book
    2. How to Use This Book
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  2. 1. Introduction: Why ClojureScript?
    1. The Rise of Browser Applications
    2. The Rise of JavaScript
    3. The Need for a Better Language
    4. Introducing ClojureScript
  3. 2. Hello World
    1. Leiningen
      1. Installing Leiningen on OS X and Linux
      2. Installing Leiningen on Windows
    2. Using lein-cljsbuild
      1. Getting Started with the REPL
      2. Compiling a ClojureScript File to JavaScript
        1. Structuring the Leiningen project
        2. Updating the project configuration
        3. Writing a ClojureScript file
        4. Compiling
      3. Running ClojureScript in the Browser
      4. Other Capabilities of lein-cljsbuild
  4. 3. The Compilation Process
    1. Architecture
      1. Google Closure Compiler
      2. The Google Closure Library
      3. ClojureScript and Google Closure
      4. The Compilation Pipeline
    2. How to Compile
      1. Compiling ClojureScript
        1. Hello, Compiler
        2. The Clojure REPL
    3. Compilation in Depth
      1. Compilation Sources
      2. Compilation and Optimization Options
        1. :optimizations
        2. Where do the files go?
        3. Compiling with optimizations
        4. Loading optimized code in a browser
        5. Compiling without optimizations
        6. Loading unoptimized code in a browser
        7. An alternative: pretty-printing
      3. Other Compilation Options
    4. Summary
  5. 4. ClojureScript Basics
    1. ClojureScript versus Clojure
    2. Expressions and Side Effects
    3. Syntax and Data Structures
      1. Symbols and Keywords
      2. Data Structures
    4. Special Forms and Definitions
    5. Functions
      1. Multi-Arity Functions
      2. Variadic Functions
    6. Local Bindings
      1. Destructuring
    7. Closures
    8. Flow Control
      1. Conditional Branching
        1. cond
        2. do
        3. when
    9. JavaScript Interop
      1. The js Namespace
      2. Methods and Fields
      3. Constructor Functions
      4. Scope of this
      5. Exceptions
    10. Summary
  6. 5. Data and State
    1. Primitives
      1. Strings
      2. Keywords
      3. Symbols
      4. Characters
      5. Numbers
      6. Booleans
      7. Functions
      8. nil
    2. Data Structures
      1. Collection Types
        1. Lists
        2. Vectors
        3. Maps
        4. Sets
      2. Immutability
        1. Why immutability?
      3. Persistence
    3. Identity and State
      1. Atoms
  7. 6. Sequences
    1. The Sequence Abstraction
    2. Lazy Sequences
      1. Letting Go of the Head
    3. The Sequence API
      1. map
      2. reduce
      3. filter
      4. Other Useful Sequence Functions
  8. 7. Namespaces, Libraries, and Google Closure
    1. Namespaces
      1. Using Namespaces
        1. Using namespaces at the REPL
      2. Using Namespaces Effectively
      3. The Implementation of Namespaces
        1. Namespaces and *.js files
        2. Namespaces and variable names
    2. Advanced Compilation Mode
    3. Consuming Libraries
      1. ClojureScript Libraries
      2. JavaScript Libraries
        1. Google Closure libraries
        2. Plain old JavaScript libraries
          1. With Advanced mode
          2. Without Advanced mode
    4. Creating Libraries
      1. For Consumption by ClojureScript
      2. For Consumption by JavaScript
        1. ^:export metadata
  9. 8. Macros
    1. Code as Data
    2. Writing Macros
      1. Syntax-Quote
      2. Auto-Gensyms
    3. Using Macros
    4. When to Write Macros
    5. Summary
  10. 9. Development Process and Workflow
    1. Installing ClojureScript
      1. Checking Out from Source Control
      2. Downloading a Compressed Archive
      3. Installing Dependencies
    2. The Built-In Tools
      1. Command-Line Compilation
      2. Clojure REPL
      3. ClojureScript REPL
    3. The Browser REPL
      1. Setting Up the Browser REPL
        1. Serving your HTML via Ring and Compojure
        2. Starting the bREPL server
        3. Configuring the bREPL client
    4. Additional lein-cljsbuild Features
      1. Launching a Browser REPL
      2. Custom bREPL Launch Commands
      3. Hooking Into Default Leiningen Tasks
      4. Testing ClojureScript Code
      5. Including ClojureScript in JAR Files
      6. Compiling the Same Code as Clojure and ClojureScript
  11. 10. Integration with Clojure
    1. AJAX
    2. The Reader and Printer
    3. Example Client-Server Application
    4. Extending the Reader
      1. User-Defined Tagged Literals
    5. Sharing Code
    6. Summary
  12. A. Libraries
    1. ClojureScript’s Standard Library
    2. Google Closure Library
    3. Domina
    4. Enfocus
    5. Jayq
    6. C2
    7. core.logic
  13. About the Authors
  14. Copyright