Appendix A. Libraries

Hopefully, this book has helped you to understand the basics of ClojureScript and how it works. But in order to build a real ClojureScript application, you’ll need more than the basics. You’ll need to manipulate the DOM. You’ll need to send and receive AJAX requests and long-poll messages. You’ll need to do drawing and visualizations.

Any one of these topics can be (and is) the subject of its own book, and unfortunately there isn’t room to cover them all adequately here. However, you already have all the tools you need: with ClojureScript’s capability to interoperate with JavaScript, you can access any JavaScript browser API, and consume any JavaScript library.

Additionally, despite the relative youth of ClojureScript, there is already a healthy crop of libraries designed not only to be compatible with ClojureScript, but to follow ClojureScript idioms and fit seamlessly into your application. Here, we will briefly introduce several of them to help give you an idea of the landscape.

ClojureScript’s Standard Library

Lots of things you might want to do are actually already included in ClojureScript. There are many library namespaces available besides just cljs.core.

cljs.reader

We covered the reader in Chapter 10. Unlike Clojure’s reader, the ClojureScript reader lives in a separate namespace.

clojure.set

Contains set manipulation functions such as union, intersection, difference, etc.

clojure.string

Contains a variety of useful string manipulation functions, including split, join, and replace.

clojure.walk

Contains tools for recursively walking and manipulating nested data structures in a functional style.

clojure.zip

Contains an efficient implementation of zippers, a useful data structure for fully-functional yet performant tree navigation and manipulation.

Google Closure Library

Although the Google Closure library isn’t itself written in ClojureScript, it is still extremely easy to use from ClojureScript. In fact, it’s bundled with the ClojureScript distribution, and ClojureScript’s standard library itself uses it. All you need to use it is to :require the namespace you want—it’s already on your project’s compile classpath.

The library itself is quite large. It contains over 50 namespaces, ranging from basic low-level functionality all the way up to elaborate UI widgets. Documentation can be found here.

For ClojureScript, some pieces of the library are more useful than others. Components written in a functional style (such as most of string, math, dom, and crypt) fit into any ClojureScript application seamlessly.

Others, like most of the UI code, are written in a very object-oriented style that, while completely usable from ClojureScript, are less convenient. These libraries will require a bit more work to fit into a ClojureScript program cleanly, but can provide powerful capabilities that would be extremely time-consuming to rewrite in pure ClojureScript.

Domina

Domina is a DOM manipulation library for ClojureScript aiming to provide basic DOM manipulation capabilities in a convenient and idiomatic way. It is cross-browser, wrapping the DOM component of the Google Closure library.

It is loosely inspired by jQuery in that it supports easy querying to retrieve sets of nodes, and its operations usually accept and return node sets. The major difference from JQuery is that the concept of a “NodeSet” (called “DomContent” in Domina) is not a reified object, but a polymorphic abstraction over a variety of native concrete types including HTML NodeLists, individual nodes, arrays of nodes, XPath or CSS selections, etc.

In theory, this allows it to be more composable, allowing other libraries to supply new operations on DomContent, or create implementations of it against new concrete types.

Enfocus

Enfocus is another excellent DOM manipulation and templating library. It focuses on higher-level manipulations such as transformations, events, and animation effects, and is heavily influenced by the popular Enlive templating library for Clojure.

Enfocus is intended to be complimentary to Domina—in fact, Enfocus builds its high-level transformations on top of the lower-level features Domina provides.

Jayq

Jayq is a thin but quite complete wrapper for the extremely popular JQuery library for JavaScript. It allows users to leverage JQuery’s DOM manipulation capabilities, cross-browser compatibility, and large mindshare in ClojureScript.

If you are familiar and comfortable with JQuery, then Jayq is likely to be a good choice for you.

C2

C2 is a powerful data visualization library for ClojureScript inspired by the excellent D3 JavaScript library. It provides mechanisms for making declarative mappings from your ClojureScript data to interactive, dynamic visualizations such as charts, graphs, and maps.

It supports a heavily data-driven approach, meaning that once a data binding is established between ClojureScript data structures and visual elements, the visual elements can watch the data and update automatically based on changes.

If you intend to do any charting, graphing, or other graphical data representation in ClojureScript, C2 is worth a very close look.

core.logic

Originally built for Clojure, core.logic is an extremely powerful logic/relational/declarative programming system that now supports ClojureScript as well. It is based on miniKanren, a logic programming system for Scheme invented by Daniel Friedman, William Byrd, and Oleg Kiselyov and explained in the wonderful book The Reasoned Schemer (MIT Press).

Logic programming allows users to express a problem declaratively, letting the implementation worry about the actual steps required to compute a solution. For certain classes of problems, this approach can be extremely concise and elegant.

Although the learning curve for logic programming can be steep, exploring core.logic is well worth it not just for the practical benefit to your programs, but as an educational tool to help you think about software development itself in new ways.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.38.14