
Professional Clojure
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
More details
Other editions
Additional editions


Persons
Content
INTRODUCTION xv
CHAPTER 1: HAVE A BEGINNER'S MIND 1
Functional Thinking 2
Value Oriented 2
Thinking Recursively 5
Higher Order Functions 8
Embracing Laziness 11
When You Really Do Need to Mutate 12
Nil Punning 15
The Functional Web 16
Doing Object-Oriented Better 16
Polymorphic Dispatch with defmulti 18
Defining Types with deftype and defrecord 20
Protocols 21
Reify 22
Persistent Data Structures 23
Shaping the Language 27
Summary 29
CHAPTER 2: RAPID FEEDBACK CYCLES WITH CLOJURE 31
REPL-Driven Development 32
Basic REPL Usage with Leiningen 32
Remote REPLs with nREPL 34
REPL Usage with a Real Application 35
Connecting Your Editor to a REPL 39
Reloading Code 40
Reloading Code from the REPL 40
Automatically Reloading Code 43
Writing Reloadable Code 49
Summary 51
CHAPTER 3: WEB SERVICES 53
Project Overview 53
Namespace Layout 54
Elements of a Web Service 55
Libraries, Not Frameworks 55
HTTP 55
Routing 64
JSON Endpoints 70
Example Service 74
Create the Project 75
Additional Namespaces 75
Default Middleware 77
The Storage Protocol 78
Handlers 83
Middleware 88
Routes 89
Deployment 94
Using Leiningen 94
Compiling an Uberjar or Uberwar 95
Hosting 96
Summary 97
CHAPTER 4: TESTING 99
Testing Basics with clojure.test 100
with-test 101
deftest 101
are 102
Using Fixtures 103
Testing Strategies 104
Tests Against DB 104
Testing Ring Handlers 106
Mocking/Stubbing Using with-redefs 108
Redefining Dynamic Vars 110
Record/Replay with VCR 111
Measuring Code Quality 112
Code Coverage with Cloverage 112
Static Analysis with kibit and bikeshed 114
Keeping Dependencies Under Control 116
Testing Framework Alternatives 119
Expectations 119
Speclj 119
Cucumber 120
Kerodon 126
Summary 127
CHAPTER 5: REACTIVE WEB PAGES IN CLOJURESCRIPT 129
ClojureScript Is a Big Deal 129
A First Brush with ClojureScript 131
Starting a New ClojureScript Project 132
Getting Fast Feedback with Figwheel 132
Creating Components 134
Modeling the Data 135
Responding to Events and Handling State Change 136
Understanding Errors and Warnings 137
Namespace Layout 141
Styling 141
Form Inputs and Form Handling 142
Navigation and Routes 145
HTTP Calls: Talking to a Server 147
Drag and Drop 149
Publishing 150
Reagent in Depth 151
Form 1: A Function That Returns a Vector 151
Form 2: A Function That Returns a Component 152
Form 3: A Function That Returns a Class 153
Sequences and Keys 154
Custom Markup 155
Reactions 156
A Note on Style 158
Testing Components with Devcards 159
Interop with JavaScript 162
One Language, One Idiom, Many Platforms 164
Things to Know About the Closure Compiler and Library 164
Modeling State with DataScript 165
Go Routines in Your Browser with core.async 166
Summary 167
CHAPTER 6: THE DATOMIC DATABASE 169
Datomic Basics 170
Why Datomic? 170
The Datomic Data Model 172
Querying 175
Transactions 181
Indexes Really Tie Your Data Together 183
Datomic's Unique Architecture 187
Modeling Application Data 188
Example Schema for Task Tracker App 188
Entity ids and Partitions 196
Datomic's Clojure API 197
Basic Setup 197
Experimenting in the REPL 200
Building Applications with Datomic 206
User Functions 206
Account Functions 209
Task Functions 210
Deployment 213
The Limitations 214
Summary 215
CHAPTER 7: PERFORMANCE 217
What Is Performance? 219
Choosing the Right Data Structure Is a Prerequisite for Performance 219
Benchmarking 221
Timing Slow Things 221
Use Criterium for Timing Fast Things 223
Use Test Selectors for Performance Tests 225
Parallelism 225
Memoization 226
Inlining 227
Persistent Data Structures 228
Safe Mutation with Transients 228
Profi ling 229
Avoiding Reflection with Type Hinting 230
Java Flags 232
Math 232
Summary 232
INDEX 235
INTRODUCTION
WHAT IS CLOJURE?
Clojure is a dynamic, general-purpose programming language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. Clojure is a compiled language, yet remains completely dynamic-every feature supported by Clojure is supported at runtime. Clojure provides easy access to the Java frameworks, with optional type hints and type inference, to ensure that calls to Java can avoid reflection.
Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system. Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures. When mutable state is needed, Clojure offers a software transactional memory system and reactive Agent system that ensure clean, correct, multithreaded designs.
-Rich Hickey, author of Clojure
This quote from Rich Hickey, the creator of Clojure, captures what Clojure is. Many people equate Clojure with functional programming, but much like Lisp, its predecessor, it's a general-purpose language that will support you no matter what paradigm you decide to program in.
Clojure is, however, very opinionated and offers great support for programming in a functional manner, with its focus on immutable values and persistent data structures. You may be surprised to know that Clojure also offers the ability to do object-oriented programming, which we cover in this book.
WHO IS THIS BOOK FOR?
This book was written with the professional programmer in mind. This means you should have experience programming in a language, and you should know the basic syntax and concepts in Clojure, and be ready to take Clojure programming to the next level. Our goal is to take you from a Clojure beginner to being able to think like a Clojure developer. Learning Clojure is much more than just learning a new syntax. You must use tools and constructs much differentely than anything you may be familiar with.
DEMO APPLICATION SOURCE CODE
You can access the source code from the Wiley website at www.wiley.com/go/professionalclojure or at our demo application via Github at https://github.com/backstopmedia/clojurebook.
A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize our ideas about processes.
-Structure and Interpretation of Computer Programs
This book assumes some prior knowledge of Clojure and programming in general, but does not assume proficiency in Clojure. It will cover a broad scope of topics from changing the way you think and approach programming to how you integrate the REPL into your normal development routine to how you build real world applications using Ring and ClojureScript.
WHAT WILL YOU LEARN?
Our goal is to provide you with some real world examples of how to apply your Clojure knowledge to your day-to-day programming, not just theory and academia.
Chapter 1
In Chapter 1, you will learn about Clojure's unique view on designing programs. You'll discover some of the things that set Clojure apart from other languages, for example, how immutability is the default, and how Clojure qualifies as object-oriented programming.
Chapter 2
In Chapter 2, you will learn how to become proficient with the REPL and various tips and techniques for interacting with your actual application through the REPL. You'll learn how to run your code and tests from the REPL as well as how to write code that is easily reloaded from the REPL without having to restart it.
Chapter 3
In Chapter 3, you learn about building web services with Compojure, and the various concepts involved such as routes, handlers, and middleware. You will build a complete web service, and then learn various techniques for deploying your new application.
Chapter 4
Chapter 4 covers testing in Clojure, focusing primarily on the clojure.test testing library. You'll learn various techniques for many common testing scenarios, along with tools to help measure the quality of your code.
Chapter 5
In Chapter 5, you will learn how to build a task management web application similar to the popular Trello application in ClojureScript. You'll also learn the techniques for sharing functions between both your server-side and client-side applications.
Chapter 6
Chapter 6 takes a look at Datomic and how it applies the concept of immutability to databases. You'll learn the basics of how to model data in a Datomic database and how to extract that information. Then you'll apply this knowledge to building a database to support the task management application from Chapter 5.
Chapter 7
In Chapter 7, you'll take a look at performance and how to make your Clojure code execute faster. You'll discover how with a little work you can tweak your Clojure code to be as fast as Java code.
TOOLS YOU WILL NEED
Just as in any good adventure or journey, having the right tools makes things go much smoother. Fortunately, to work through the examples in this book, you only need three things: Java, Leiningen, and a good text editor.
Java
Most computers these days come with Java pre-installed, but in order to run the examples contained in this book you need to make sure you have installed a recent version. The code examples in this book were written with and confirmed to work with JDK 1.8.0_25. For instructions on how to download and install the proper JDK for your platform, see the documentation at Oracle's JDK download page: (http://www.oracle.com/technetwork/java/javase/downloads/index.html).
Leiningen
Leiningen, according to their website (http://leiningen.org), is the most contributed-to Clojure project. For those of you coming from a background in Java, Leiningen fills a similar role that Maven does for the Java world, only without all of the XML, and you can avoid wanting to pull your hair out. It helps you manage the dependencies for your project and declaratively describe your project and configuration, and provides access to a wealth of plugins for everything from code analysis to automation, and more. Leiningen makes your Clojure experience much more enjoyable.
Fortunately, getting Leiningen up and running is a fairly simple task. You'll want to install the latest version available, which at the time of this writing is 2.5.3. Please refer to the Leiningen website for instructions particular to your programming environment.
Editors
Once you have Leiningen installed, the only thing left to do is to make sure you have a good text editor to efficiently edit your Clojure code. If you have a favorite editor, just use what you're already comfortable with. However, if your editor doesn't support basic things like parentheses balancing, integration with the REPL, syntax highlighting, or properly indenting Clojure code, you may want to consider one of the editors below.
Emacs
Emacs is the favored editor of many grizzled veterans. It has a long history with Lisp. Even though it has a steep learning curve, it is considered by many to be very powerful, and no other editor is as extensible. There are many custom Emacs configurations designed to help ease the learning curve, such as Emacs Prelude (https://github.com/bbatsov/prelude), which also contains a sensible default configuration for developing in many languages, including Clojure.
LightTable
LightTable (http://lighttable.com) began life as a Kickstarter project with a unique new vision of how to integrate the code editor, REPL, and documentation browser for Clojure. It has delivered on those promises and then some and has gained popularity among many in the Clojure community.
Cursive (IntelliJ)
If you're already comfortable with using any of the various JetBrains IDEs, you'll be happy to know that there is a plugin for IntelliJ called Cursive (https://cursive-ide.com). Besides having good integration with nREPL, it also stays true to its reputation and contains excellent refactoring support, as well as debugging and Java interop.
Counterclockwise (Eclipse)
For those who are familiar with Eclipse, there is Counterclockwise (http://doc.ccw-ide.org), which can be installed as either an Eclipse plugin or a standalone product. Counterclockwise boasts many of the same features as the previous editors, integration with the REPL, and ability to evaluate code inline.
CONVENTIONS
To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.
NOTE
Notes indicates notes, tips, hints, tricks, and/or asides to the current discussion.
As for styles in the text:
- We highlight new terms and important words when we introduce them.
- We show code within the text like so:
persistence.properties. - We show all code snippets in the book using this style:
FileSystem fs = FileSystem.get(URI.create(uri), conf); InputStream in = null; try...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.