Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Learning ClojureScript
Learning ClojureScript

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

Arrow left icon
Profile Icon Jarvis Profile Icon Allen Rohner
Arrow right icon
Can$30.99 Can$44.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4 (7 Ratings)
eBook Jun 2016 320 pages 1st Edition
eBook
Can$30.99 Can$44.99
Paperback
Can$55.99
Subscription
Free Trial
Arrow left icon
Profile Icon Jarvis Profile Icon Allen Rohner
Arrow right icon
Can$30.99 Can$44.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4 (7 Ratings)
eBook Jun 2016 320 pages 1st Edition
eBook
Can$30.99 Can$44.99
Paperback
Can$55.99
Subscription
Free Trial
eBook
Can$30.99 Can$44.99
Paperback
Can$55.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learning ClojureScript

Chapter 2. ClojureScript Language Fundamentals

ClojureScript provides the developer with great expressive power, thanks to its elegant semantics and its bias toward functional programming-both artifacts of its heritage as a Lisp language.

It also encourages the use of a minimal set of pragmatic and efficient data structures that make it possible to productively write data-oriented programs that are bound by a set of unified operations, typically conforming to the powerful seq abstraction.

In this chapter, we are going to explore these semantics and data structures so that we can gain a deep knowledge of the ClojureScript language basics. We'll delve into its basic scalar types and collections, understand how expression evaluation and variable scoping work, study the seq abstraction along with the concept of laziness, and show how interoperability with JavaScript works. We'll be covering the following topics:

  • Understanding ClojureScript functions
  • ClojureScript data structures...

Understanding ClojureScript functions

Before we dive too far into ClojureScript, we need to understand the syntax behind ClojureScript functions. Functions in ClojureScript work like functions in most computer languages.

Functions

Functions in ClojureScript are first-class entities, which means that we can store them as variables, or values in data structures, return them as values from other functions, and pass functions as arguments to other functions.

We'll be demonstrating quite a bit of code evaluation in this chapter. In order to follow along, start up an REPL following the instructions provided in the previous chapter.

Let's start by quickly seeing what a function call in ClojureScript looks like at the REPL:

cljs.user=> (+ 1 2)
;; => 3

ClojureScript, as a Lisp, looks unlike many other languages, including JavaScript. It is a language written in prefix notation, which means that the calling function is the first argument. The same operation, performed in JavaScript, would...

The ClojureScript data structures

ClojureScript shares all of Clojure's basic scalar types, but due to the difference in runtime platform, it relies on different underlying mechanics for implementation. Let's begin by quickly reviewing the basic language types first.

Scalar types

As with Clojure, scalars in ClojureScript are directly linked to the host platform. In this case, this means that ClojureScript scalars are just basic JavaScript types.

Numbers

ClojureScript numbers are nothing but JavaScript numbers. Type at your REPL the following:

cljs.user> (type 3)
;; => #object[Number "function Number() {
  [native code]
}"]

Unlike Clojure, this is true for all numeric types, whereas Java breaks numeric types into different types like Bigint, Integer, Float and Double, and all numeric types in ClojureScript are just JavaScript numbers:

cljs.user> (type 1.1)
;; => #object[Number "function Number() {
  [native code]
}"]
cljs.user> (type
      5729348720938479023874928374982734982735982374928734928735982...

Immutability

Now that you've had a basic introduction to ClojureScript's data structures, let's talk a bit about immutability. Almost all of ClojureScript's data types are immutable, which means that once they're defined, including them in an expression won't change their underlying value. This concept can take a bit of getting used to, so let's take a look at a few examples. As a point of contrast, we'll use JavaScript as an example of a language where data types are mutable.

Let's start with an example using a vector. First, we'll define a vector with one element in it, the integer 1:

cljs.user=> (def x [1])
;; => #'cljs.user/x

Now, we'll call conj on x. We've already talked a bit about how conj works earlier in this chapter, but just to review, the conj function returns a new vector that consists of the original vector with any of the following arguments added to the original vector:

cljs.user=> (conj x 2)
;...

Advanced destructuring and namespaces

In this section, we'll dig further into ClojureScript's destructuring syntax. We'll also learn about ClojureScript namespaces. If you're familiar with JavaScript ES6 modules, namespaces are sort of akin to that-they're essentially modules within which variable and function definitions are located and a collection of imported libraries can be defined, often with local bindings for convenience.

Destructuring

Destructuring in ClojureScript provides a way of binding values to local variables. We've already seen a few simple examples of how this works with the code in previous sections, but destructuring in ClojureScript is extremely powerful and so comprehensive that it's worth looking at some more advanced patterns of it.

First, let's try destructuring the vector [1 2]:

cljs.user=> (let [[a b] [1 2]] (+ a b))
;; => 3

The same destructuring logic works in a nested fashion:

cljs.user=> (let [[[a b] c] [[1 2] 3...

JavaScript interoperability

One of the most powerful things about ClojureScript is the ease with which one can access and interact with the JavaScript runtime. In this section, we'll take a closer look at how you can work with native JavaScript code from ClojureScript.

JavaScript collections

Odds are good that you won't want to work too much with JavaScript collections directly now that you've gotten an understanding of how powerful ClojureScript's collection objects are, but it's still important to know how to access these from ClojureScript as well as to make sure you're comfortable converting JavaScript data types to ClojureScript and vice versa. Learning about this syntax will also prove useful when calling JS libraries from ClojureScript.

Arrays

Following is an example of defining and then accessing a JavaScript array from the ClojureScript REPL:

cljs.user> (def a (array 1 2 3))
;; => #'cljs.user/a
cljs.user=> a
;; => #js [1 2 3]
cljs.user...

Understanding ClojureScript functions


Before we dive too far into ClojureScript, we need to understand the syntax behind ClojureScript functions. Functions in ClojureScript work like functions in most computer languages.

Functions

Functions in ClojureScript are first-class entities, which means that we can store them as variables, or values in data structures, return them as values from other functions, and pass functions as arguments to other functions.

We'll be demonstrating quite a bit of code evaluation in this chapter. In order to follow along, start up an REPL following the instructions provided in the previous chapter.

Let's start by quickly seeing what a function call in ClojureScript looks like at the REPL:

cljs.user=> (+ 1 2)
;; => 3

ClojureScript, as a Lisp, looks unlike many other languages, including JavaScript. It is a language written in prefix notation, which means that the calling function is the first argument. The same operation, performed in JavaScript, would look like...

Left arrow icon Right arrow icon

Key benefits

  • 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

Description

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.

Who is this book 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 30, 2016
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781785887796
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 30, 2016
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781785887796
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Can$ 195.97
Clojure Programming Cookbook
Can$69.99
Learning ClojureScript
Can$55.99
Mastering Clojure
Can$69.99
Total Can$ 195.97 Stars icon
Banner background image

Table of Contents

8 Chapters
1. Getting Ready for ClojureScript Development Chevron down icon Chevron up icon
2. ClojureScript Language Fundamentals Chevron down icon Chevron up icon
3. Advanced ClojureScript Concepts Chevron down icon Chevron up icon
4. Web Applications Basics with ClojureScript Chevron down icon Chevron up icon
5. Building Single Page Applications Chevron down icon Chevron up icon
6. Building Richer Web Applications Chevron down icon Chevron up icon
7. Going Further with ClojureScript Chevron down icon Chevron up icon
8. Bundling ClojureScript for Production Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(7 Ratings)
5 star 85.7%
4 star 0%
3 star 0%
2 star 0%
1 star 14.3%
Filter icon Filter
Top Reviews

Filter reviews by




Daniel Woelfel Mar 07, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have quite a bit of experience building webapps in ClojureScript. I was the main author of CircleCI's cljs frontend and I cofounded PrecursorApp, a collaborative prototyping app with a cljs frontend. Both codebases are open-source on GitHub (circleci/frontend and PrecursorApp/precursor).I wish I would have had this book when I started working with ClojureScript. ClojureScript has one of the best developer experiences, with code-reloading and a REPL, but it can be a bit daunting to set it up the first time. This book takes you through all the steps and keeps you from missing out on some of the best aspects of developing with ClojureScript.The second and third chapters might be a bit too much to take in at once for people new to ClojureScript. I'd recommend you skim them the first time, then come back and use them as a reference when you start building your application.The rest of the book gives you a nice foundation for building applications in ClojureScript. I may have focused on a few different libraries, but the ones they cover are pretty solid, with one notable exception. I wouldn't recommend anybody use Dommy. I used it and found it very confusing--you're much better off using the tools you get for free from Google Closure.There's lots of other great stuff in the book. I'll definitely be coming back to the section on ClojureScript modules--Precursor ships with a 1.5mb (only 388k gzipped) javascript file and I'd love to get that down to a more reasonable size.
Amazon Verified review Amazon
Emergent Iguana Dec 23, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
In case you are even vaguely curious about ClojureScript and are wondering whether or not to learn it, I strongly encourage you to do so, as it has become obvious to me that it is the future of front-end web development. (Technically, it is one of several futures, but as it stands it has several years head start over most options except for CoffeeScript and of course native JavaScript. I have never programmed in CoffeeScript but having around 15 years experience with JavaScript I can safely tell you that ClojureScript provides a great many benefits to any programmer over JS, and that is even taking into account ES6 which does introduce a few of the benefits that have been native to ClojureScript since its inception.)I have been programming in ClojureScript for over a year now, and this book, which I bought last Christmas (directly in the Packt sale for a mere $5) helped my learning curve tremendously.The most important first step in learning any new language is setting up your development environment, and this book does an excellent job in covering this for ClojureScript. While it goes through a number of different scenarios, and this can seem confusing at first, by far the best option is to use Figwheel, which provides an as close to real-time live encoding flow as you are going to get for a compile-to-JS language.It also covers setting up Emacs as an IDE, which is probably at the very least the best editor (rather than IDE) available for programming in LISP dialects (of which ClojureScript is one), mostly due its paredit plugin. (I am much more of a Vim user but sadly I haven't found any plugin in Vim which comes close to the functionality that the emacs paredit plugin provides.)Chapters 2 and 3 cover the language fundamentals and some advanced features, including interop with JS and macros. The treatment of macros is adequate enough, however some more could have been written about them. Essentially, they allow you to write ClojureScript code which is rewritten, via macros, into some other ClojureScript code, which is ultimately compiled into JS. This is one of the most powerful features of the language, and once you have more experience with the language you will definitely want to write macros in certain situations where it makes sense.One area which is not covered adequately by the book is the ClojureScript and Clojure library core.async. This provides an amazing alternative way of handling events to that which is native to JS / the DOM. While events are still present when using core.async, event handlers become much more lightweight and the majority of event handling code is sent of to something known as a "go channel" which takes care of the bulk of work required, including any state management. There are a couple of excellent videos, one on youtube and one on Cognitect's website (entitles "core.async webinar") which show you some of the amazing things which you can achieve using core.async.Chapter 4 covers a decent selection of libraries which allow you to work with the DOM and do general web development, all of which are worth looking at if you are not using React.Chapter 5 covers React development, and herein is another major flaw of the book. There are several ClojureScript libraries available for working with React, and the book's authors chose to go with Om. I tried using Om following the guidelines in this book, together with the first two tutorials on the Om github page, and to cut a long story short, I couldn't get it to do what simple things I was trying to achieve. I found myself having to bend over backwards to fit my ideas into the Om framework, and after much hair-pulling, I gave up.Instead, I discovered Reagent, which is an amazing framework for building React applications using ClojureScript, not least of all because it is incredibly simple to use. I have been using Reagent for several months now and I have absolutely zero complaints, in short, it is an amazing framework and I cannot recommend it enough.Beyond this I have only read the section on testing, which again covers testing using the native cljs.test library more than adequately enough. However you should be aware that the developer of Figwheel created something called Devcards, which is designed primarily to test React components outside of their native application contexts, but also includes thorough testing capabilities which reports results in your browser rather than in a command-line session. If you search the web for "TDD in ClojureScript" you will find an article by Eric Smith which explains how to do this, it's well worth going through it all for the benefits you'll get.This is by far the best book available on ClojureScript at this time (there are only 3 others and one of those only covers Reagent), and I would highly recommend anyone starting out or with just a couple of month's experience in ClojureScript to read it and learn from it, it provides an incredibly invaluable resource to what I consider to be the most useful language for front-end web-development today.
Amazon Verified review Amazon
hswolff Aug 22, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a great and thorough introduction to ClojureScript. It makes no assumptions on what you may already know. Instead it focuses on building a strong foundation of knowledge. Two whole chapters are focused on just making sure you have a strong understanding of what ClojureScript is and what makes it different from JavaScript. That was invaluable as I went further into the book. From there the book teaches how to create a web application with React by way of Om. And rather than leave me high and dry with just a fun toy application the book wraps things up by teaching how to move code to a production environment. Definitely a strong recommend if you're interested in going from a ClojureScript beginner to a pro!
Amazon Verified review Amazon
DUPUCH Nov 21, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Pros :- Start from the basics and go through complex cases- Well written, can be seriously read in a 2/3 days period (if you have a little experience in clojure(script)- RecentCons:- Code examples in chapter 5 (om todo app with back-end) - some mistakes in code
Amazon Verified review Amazon
A Reader Aug 15, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
A very clear introduction to developing with Clojurescript. The pacing was excellent. The examples made a ton of sense. I really liked how the author included links and tips to upcoming tech as well (such as the js compiler.)
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.