Download Living Clojure PDF

TitleLiving Clojure
Author
LanguageEnglish
File Size4.7 MB
Total Pages325
Table of Contents
                            Preface
	Who This Book Is For
	How to Use This Book
		Do Try the Examples
		Don’t Feel Overwhelmed
		Don’t Worry About the Parens
		One More Thing—Have Fun!
	What You Need to Use This Book
		Install Java
		Getting Your Clojure REPL Ready
	Conventions Used in This Book
	Using Code Examples
	Safari® Books Online
	How to Contact Us
	Acknowledgments
I. A Guided Tour of Clojure
1. The Structure of Clojure
	Baby Steps with Simple Values
	Put Your Clojure Data in Collections
		Using a List Collection
		What Can We Do with Lists?
		Using Vectors for Collecting Data by Index
		What Collections Have in Common
		Maps for Storing Key-Value Pairs of Data
	Using Sets for Unique Collections of Data
		Summary of Simple Value and Collections
	Lists Are the Heart of Clojure
	Symbols and the Art of Binding
	Creating Our Own Functions
		Keep Your Symbols Organized in Namespaces
2. Flow and Functional Transformations
	Controlling the Flow with Logic
		Logic Tests You Can Use on Collections
		Harnessing the Power of Flow Control
	Functions Creating Functions and Other Neat Expressions
	Destructuring
	The Power of Laziness
	Recursion
	The Functional Shape of Data Transformations
		Map the Ultimate
		Reduce the Ultimate
		Other Useful Data Shaping Expressions
3. State and Concurrency
	Handling Real-World State and Concurrency
		Using Atoms for Independent Items
		Using Refs for Coordinated Changes
		Using Agents to Manage Changes on Their Own
4. Java Interop and Polymorphism
	Handling Interop with Java
	Practical Polymorphism
5. How to Use Clojure Projects and Libraries
	Getting Set Up with a Clojure Editor
	Creating Our Project with Leiningen
	Dependency Management with Leiningen
	Using Libraries in Your Own Project
6. Communication with core.async
	Basics of core.async Channels
	Serving Tea at a core.async Tea Party
	Creating a Tea Party to Be Run from the Command Line
		Sharing Your Tea Party with Others by Creating an Uberjar
7. Creating Web Applications with Clojure
	Creating a Web Server with Compojure
	Using JSON with the Cheshire Library and Ring
	Using Clojure in Your Browser with ClojureScript
	Browser-Connected REPL
	Making HTTP Calls with ClojureScript and cljs-http
	DOM Control with ClojureScript and Enfocus
	Event Handling with Enfocus
	Summary of Our Clojure and ClojureScript Web Application
	Other Useful Web Development Libraries
		Using Hiccup for Templating
		Using Enlive for Templating from Static HTML Files
		Using Liberator for Content Negotiation and Other Good Things
		Using Transit for a Small, Fast JSON Alternative
		Using Om for Powerful Client-Side Applications
		Using Hoplon and Luminus for Inclusive, Bundled Libraries for Web Development
		Dealing with Databases
8. The Power of Macros
	Exploring Macros
	Creating Our Own Macros
	Using Templating to Create Macros
II. Living Clojure Training Program
9. Joining the Clojure Community
	Clojure Documentation
		ClojureDocs
		Grimoire
		Going to the Source
	Which Libraries to Use
		Clojars
		CrossClj
		ClojureWerkz and the Clojure Toolbox
	Clojure News
	Finding Other Clojurists
		Chat
		Conferences
	Getting Help with Problems and Questions
	Building Things with Other Clojure Enthusiasts
	Summary
10. Weekly Living Clojure Training Plan
	How Do I Use This Training Plan?
	What If I Miss a Day or Two?
	What If I Don’t Understand the Exercise?
	Week 1
	Week 2
	Week 3
	Week 4
	Week 5
	Week 6
	Week 7
	Congratulations
11. Further Adventures
	Get Ready for Transducers
	Further Reading
Index
                        
Document Text Contents
Page 162

:pretty-print makes the output more human readable — always a good thing in
development.

You whole project.clj file should now look like this:

(defproject cheshire-cat "0.1.0-SNAPSHOT"

:description "FIXME: write description"

:url "http://example.com/FIXME"

:min-lein-version "2.0.0"

:dependencies [[org.clojure/clojure "1.6.0"]

[compojure "1.3.1"]

[ring/ring-defaults "0.1.2"]

[ring/ring-json "0.3.1"]

[org.clojure/clojurescript "0.0-2371"]]

:plugins [[lein-ring "0.8.13"]

[lein-cljsbuild "1.0.3"]]

:ring {:handler cheshire-cat.core.handler/app}

:profiles

{:dev {:dependencies [[javax.servlet/servlet-api "2.5"]

[ring-mock "0.1.5"]]}}

:cljsbuild {

:builds [{

:source-paths ["src-cljs"]

:compiler {

:output-to "resources/public/main.js"

:optimizations :whitespace

:pretty-print true}}]})

Let’s try it out by getting a ClojureScript REPL up and running.

Get a terminal open in the root of the project and type in the following:

lein trampoline cljsbuild repl-rhino

This will use Rhino JavaScript to evaluate the end result from the ClojureScript that you
enter as input. It is the simplest ClojureScript REPL to get up and running.

You will see a ClojureScript REPL waiting for you:

-> lein trampoline cljsbuild repl-rhino

Running Rhino-based ClojureScript REPL.

To quit, type: :cljs/quit

ClojureScript:cljs.user>

Try something easy, like adding two numbers:

ClojureScript:cljs.user> (+ 1 1)

;; => 2

It looks just like our regular Clojure REPL, but things are really quite a bit different. Let’s
see what happens when we try to look at the class of a string:

(class "hi")

;; -> "Error evaluating:" (class "hi") ...

;; TypeError: Cannot call method "call" of undefined

We are no longer on the JVM. There are no classes here in JavaScript. But we can do

Page 163

different things. We can do interop with JavaScript functions by using a js/ prefix. For
example, we can look at the JavaScript Date function:

js/Date

;; -> #<function Date() { [native code for Date.Date, arity=1] }

We can also execute the Date function by calling it in parens:

(js/Date)

;; ->"Sun Oct 26 2014 11:27:20 GMT-0400 (EDT)"

Collections and their functions all work the same as in Clojure:

(first [1 2 3 4])

;; ->1

However, concurrency using agents and refs are not available. But we do have atoms:

(def x (atom 0))

;; -> #<Atom: 0>

(swap! x inc)

;; -> 1

It is good to remember that there are differences between Clojure and ClojureScript
although they do share core language structures and features. These are the major
differences to take note of:

ClojureScript has JavaScript interop rather than Java.

Although there are atoms, there are no agents or refs.

In ClojureScript, only integer and floating-point numbers are supported.

Of course, ClojureScript compiles to JavaScript.

TIP
ClojureScript looks very similar to Clojure, but there are some differences.

So far, we have just been experimenting in the REPL with plain ClojureScript. But we
want to be able to interact with the browser and use HTML with it. For that we need to do
a bit more setup. We will need a ClojureScript file, and of course, an HTML page to
display and call it.

In our :cljsbuild configuration, we specified that our ClojureScript files
would live in the directory of the root of our project. So, we will need to create
that directory using mkdir or your operating system’s equivalant method.

Then, go ahead a create a ClojureScript file named with the following:

Page 324

Creating Our Own Macros

Using Templating to Create Macros

II. Living Clojure Training Program

9. Joining the Clojure Community
Clojure Documentation

ClojureDocs

Grimoire

Going to the Source

Which Libraries to Use
Clojars

CrossClj

ClojureWerkz and the Clojure Toolbox

Clojure News

Finding Other Clojurists
Chat

Conferences

Getting Help with Problems and Questions

Building Things with Other Clojure Enthusiasts

Summary

10. Weekly Living Clojure Training Plan
How Do I Use This Training Plan?

What If I Miss a Day or Two?

What If I Don’t Understand the Exercise?

Week 1

Week 2

Week 3

Page 325

Week 4

Week 5

Week 6

Week 7

Congratulations

11. Further Adventures
Get Ready for Transducers

Further Reading

Index

Similer Documents