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
Free Trial
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4 (7 Ratings)
Paperback Jun 2016 320 pages 1st Edition
eBook
₱1256.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial
Arrow left icon
Profile Icon Jarvis Profile Icon Allen Rohner
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4 (7 Ratings)
Paperback Jun 2016 320 pages 1st Edition
eBook
₱1256.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial
eBook
₱1256.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781785887635
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jun 30, 2016
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781785887635
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 ₱260 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 ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 7,859.97
Clojure Programming Cookbook
₱2806.99
Learning ClojureScript
₱2245.99
Mastering Clojure
₱2806.99
Total 7,859.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.