You are previewing Learning ClojureScript.
O'Reilly logo
Learning ClojureScript

Book Description

Master the art of agile single page web application development with ClojureScript

About This Book

  • Set up interactive development workflows for the browser or Node.js thanks to the ClojureScript ecosystem

  • Learn the basics of interactive single page web app development taking advantage of the functional nature of ClojureScript

  • Delve into advanced rich web application development concepts such as Om, along with core.async, using zippers and logic programming, and preparing code for production with testing or optimizing via the Google Closure Compiler

  • Who This Book Is For

    This book is for web application developers who want to benefit from the power of ClojureScript to get an agile and highly productive development platform that targets mainly browser JavaScript.

    You are not required to be fluent in Clojure, but it will be easier for you if you have a basic understanding of browser and server-side JavaScript.

    What You Will Learn

  • Understand how the ClojureScript compiler operates

  • Set up interactive development workflows for ClojureScript

  • Grasp the basics of the ClojureScript language, including basic syntax, data structures, variable scoping, namespaces, and finally the powerful sequence abstraction

  • Delve into advanced concepts such as functional programming, macro writing, asynchronous programming, app routing, and real-time web

  • Develop simple one page web applications

  • Explore techniques to make your web apps aware of the external world through external or embedded database access or Oauth 2 integration

  • Learn more advanced ClojureScript concepts like in app routing, real-time web

  • Prepare your work for production, getting insights into optional type-checking, writing portable Clojure/ClojureScript code, and testing

  • In Detail

    Clojure is an expressive language that makes it possible to easily tackle complex software development challenges. Its bias toward interactive development has made it a powerful tool, enabling high developer productivity.

    In this book, you will first learn how to construct an interactive development experience for ClojureScript.. You will be guided through ClojureScript language concepts, looking at the basics first, then being introduced to advanced concepts such as functional programming or macro writing. After that, we elaborate on the subject of single page web applications, showcasing how to build a simple one, then covering different possible enhancements. We move on to study more advanced ClojureScript concepts, where you will be shown how to address some complex algorithmic cases. Finally, you'll learn about optional type-checking for your programs, how you can write portable code, test it, and put the advanced compilation mode of the Google Closure Compiler to good use.

    Style and approach

    This book is a comprehensive reference guide on ClojureScript development for the front end, and will gradually help you master interactive ClojureScript development workflows, through detailed step-by-step information illustrated with annotated code samples.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Learning ClojureScript
      1. Learning ClojureScript
      2. Credits
      3. Foreword
      4. About the Authors
      5. About the Reviewer
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Ready for ClojureScript Development
        1. Getting familiar with the ClojureScript ecosystem
          1. Inside the ClojureScript compiler
          2. Working with the ClojureScript REPL
        2. Live coding ClojureScript on top of nREPL with Piggieback and Weasel
          1. Working with Piggieback
          2. Setting up Weasel with PiggieBack for browser live coding
          3. ClojureScript REPLs on Node.js with Piggieback
        3. Live coding ClojureScript with Figwheel
          1. Setting up Figwheel for browser live coding
          2. Node.js interactive development workflows with Figwheel
        4. Setting integrated development environments for ClojureScript
          1. Working on Emacs with Piggieback and Weasel on CIDER
            1. Installing CIDER
            2. Working with Clojure and ClojureScript REPLs on CIDER
          2. Working on Emacs with Figwheel and inf-clojure
            1. Installing inf-clojure
            2. Configuring inf-clojure to run Figwheel as a Clojure subprocess
        5. Summary
      9. 2. ClojureScript Language Fundamentals
        1. Understanding ClojureScript functions
          1. Functions
            1. Multiarity and variadic functions
            2. Anonymous functions
            3. Side effects
            4. Local variables
        2. The ClojureScript data structures
          1. Scalar types
            1. Numbers
            2. Strings and characters
            3. Nil
            4. Boolean values and truthiness
            5. Keywords
            6. Symbols
          2. ClojureScript collections
            1. Lists
            2. Vectors
              1. When should I use lists versus vectors?
            3. Maps
              1. Different types of maps
            4. Sets
            5. Sequences
            6. Laziness
          3. Collection protocols
            1. Sequential
            2. Associative
            3. Sorted
            4. Counted
            5. Reversible
          4. Object-oriented programming
            1. Protocols
            2. Types
            3. Records
            4. Extending types and protocols
            5. Reify
          5. Other ClojureScript types
            1. Regular expressions
            2. Atoms
        3. Immutability
        4. Advanced destructuring and namespaces
          1. Destructuring
          2. Namespaces
        5. JavaScript interoperability
          1. JavaScript collections
            1. Arrays
            2. JavaScript objects
            3. JS interop syntax
          2. The Google Closure Compiler and using external JavaScript libraries
            1. Referencing external libraries with externs
            2. Bundling external libraries
              1. Google Closure Compiler compatible code
              2. Foreign JavaScript
            3. CLJSJS
        6. Summary
      10. 3. Advanced ClojureScript Concepts
        1. Functional programming concepts
          1. Loops and iteration
            1. Loop and recur functions
            2. for
            3. dotimes
            4. doseq
              1. doall
          2. Higher-order functions
            1. map
            2. The filter and remove functions
            3. reduce
          3. Transducers
        2. Control flow
          1. if and when
            1. if-let and when-let
          2. cond and condp
          3. case
          4. Exception handling
        3. Writing macros for ClojureScript
          1. read and eval
          2. Your first macro
          3. Writing more advanced macros
            1. Gensyms and local binding in macros
            2. Don't repeat yourself!
          4. A personal favorite – Threading macros
          5. A closing note on macros
        4. Concurrent design patterns using core.async
          1. JavaScript is event-driven by default
            1. Event-driven programming in ClojureScript
          2. The Communicating Sequential Processes concurrency model
            1. Getting started with core.async
            2. Background listeners
            3. Errors and core.async
        5. Summary
      11. 4. Web Applications Basics with ClojureScript
        1. Raw DOM manipulation and events handling
          1. Interacting with the browser using the Google Closure Library
        2. Dommy – An idiomatic ClojureScript library for the DOM
          1. Client-side templating in ClojureScript
          2. CSS preprocessors in ClojureScript
        3. Summary
      12. 5. Building Single Page Applications
        1. Understanding Single Page Appliactions
          1. The tradeoffs of SPAs
            1. Richer UI
            2. Easier to deal with client-side data
            3. Easier to deal with AJAX
            4. Faster client interactions once the page is loaded
            5. More development work
            6. Legacy browser support is harder
        2. Understanding Om
        3. Understanding React
          1. The React terminology
          2. The components of an SPA
            1. Setting up
            2. What just happened?
          3. Child components
          4. Items in the Om constructor signature
            1. Cursors
            2. owner
            3. Opts
            4. Input
            5. Rendering
          5. The React diffing algorithm
        4. Differences between Om and React
          1. Components
          2. State models
          3. Cursors
        5. Determining the size of a component
          1. Constructing
          2. The local state
          3. Life cycle protocols
        6. Using third-party JS
          1. jQuery listeners
        7. AJAX
          1. Dependencies
          2. The server
            1. Transit
            2. Ring
            3. CORS
            4. Data
        8. Routing and HTML5 history
          1. pushState
          2. Navigation
        9. Summary
      13. 6. Building Richer Web Applications
        1. Real-time communication with websockets
          1. Understanding the websocket protocol
          2. Initialization
          3. Sending messages
          4. Understanding Chord
        2. Using Datascript
          1. Understanding the Datascript/Datomic data model
          2. A basic query
            1. Query arguments
          3. Schema
          4. Understanding db.type/ref
          5. Cardinality
          6. Pull
          7. Finding results
            1. Unification
          8. Predicate expressions
          9. Indexes
          10. Differences between Datomic and Datascript
          11. Why Datascript?
        3. Improving load times
          1. ClojureScript modules
            1. Preparing for modules
            2. Getting started
            3. Route definition
            4. Loading modules
            5. Fixing development mode
        4. .cljc and server-side rendering
          1. .cljc
          2. Schema and input validation
          3. Server-side rendering
            1. Setting up the project
            2. Understanding Foam
        5. Summary
      14. 7. Going Further with ClojureScript
        1. Pattern matching with core.match
          1. Configuring our project
          2. Getting started with core.match
            1. Matching collections
            2. Guards and function applications
          3. Wrapping up
            1. Exploring nested data structures with clojure.zip
          4. Example – Replacing values in a tree
          5. Example – Removing values from a tree
          6. Further possibilities
            1. Declaratively solving problems with core.logic
          7. Configuring our project
          8. Getting started with core.logic
          9. Advanced core.logic
            1. Going even further
        2. Runtime data validation using schema
          1. Configuring our project
          2. A quick introduction to schema
            1. Collection schemas
            2. Function schemas
            3. Advanced schema validation
            4. Schema coercion
        3. Summary
      15. 8. Bundling ClojureScript for Production
        1. Testing your application with cljs.test
          1. Using doo for easier testing configuration
          2. Testing fixtures
          3. Asynchronous testing
            1. Asynchronous fixtures
        2. Advanced ClojureScript compilation options
          1. Compilation optimization levels
          2. Generating modules
          3. Targeting a Node.js runtime
          4. General configuration recommendations
        3. Deploying Clojure and ClojureScript applications in a Docker container
          1. Installing Docker
          2. Compiling an Uberjar
          3. Building a Docker container for our app
        4. Summary