For weeks, months—nay!—from the very moment you were born, you’ve felt it calling to you. At long last you’ll be united with the programming language you’ve been longing for: Clojure!
As a Lisp-style functional programming language, Clojure lets you write robust and elegant code, and because it runs on the Java Virtual Machine, you can take advantage of the vast Java ecosystem. Clojure for the Brave and True offers a "dessert-first" approach: you’ll start playing with real programs immediately, as you steadily acclimate to the abstract but powerful features of Lisp and functional programming. Inside you’ll find an offbeat, practical guide to Clojure, filled with quirky sample programs that catch cheese thieves and track glittery vampires.
Learn how to:
• Wield Clojure’s core functions
• Use Emacs for Clojure development
• Write macros to modify Clojure itself
• Use Clojure’s tools to simplify concurrency and parallel programming
Clojure for the Brave and True assumes no prior experience with Clojure, the Java Virtual Machine, or functional programming. Are you ready, brave reader, to meet your true destiny? Grab your best pair of parentheses—you’re about to embark on an epic journey into the world of Clojure!
Author(s): Daniel Higginbotham
Edition: 1
Publisher: No Starch Press
Year: 2015
Language: English
Commentary: Vector PDF
Pages: 328
City: San Francisco, CA
Tags: Clojure; Programming
About the Author
About the Technical Reviewer
Brief Contents
Contents in Detail
Foreword
Acknowledgments
Introduction
Learning a New Programming Language: A Journey Through the Four Labyrinths
How This Book Is Organized
Part I: Environment Setup
Part II: Language Fundamentals
Part III: Advanced Topics
The Code
The Journey Begins!
Part I: Environment Setup
Chapter 1: Building, Running,
and the REPL
First Things First: What Is Clojure?
Leiningen
Creating a New Clojure Project
Running the Clojure Project
Building the Clojure Project
Using the REPL
Clojure Editors
Summary
Chapter 2: How to Use Emacs, an Excellent Clojure Editor
Installation
Configuration
Emacs Escape Hatch
Emacs Buffers
Working with Files
Key Bindings and Modes
Emacs Is a Lisp Interpreter
Modes
Installing Packages
Core Editing Terminology and Key Bindings
Point
Movement
Selection with Regions
Killing and the Kill Ring
Editing and Help
Using Emacs with Clojure
Fire Up Your REPL!
Interlude: Emacs Windows and Frames
A Cornucopia of Useful Key Bindings
How to Handle Errors
Paredit
Continue Learning
Summary
Part II: Language Fundamentals
Chapter 3: Do Things: A Clojure Crash Course
Syntax
Forms
Control Flow
Naming Values with def
Data Structures
Numbers
Strings
Maps
Keywords
Vectors
Lists
Sets
Simplicity
Functions
Calling Functions
Function Calls, Macro Calls, and Special Forms
Defining Functions
Anonymous Functions
Returning Functions
Pulling It All Together
The Shire’s Next Top Model
let
loop
Regular Expressions
Symmetrizer
Better Symmetrizer with reduce
Hobbit Violence
Summary
Exercises
Chapter 4: Core Functions in Depth
Programming to Abstractions
Treating Lists, Vectors, Sets, and Maps as Sequences
first, rest, and cons
Abstraction Through Indirection
Seq Function Examples
map
reduce
take, drop, take-while, and drop-while
filter and some
sort and sort-by
concat
Lazy Seqs
Demonstrating Lazy Seq Efficiency
Infinite Sequences
The Collection Abstraction
into
conj
Function Functions
apply
partial
complement
A Vampire Data Analysis Program for the FWPD
Summary
Exercises
Chapter 5: Functional Programming
Pure Functions: What and Why
Pure Functions Are Referentially Transparent
Pure Functions Have No Side Effects
Living with Immutable Data Structures
Recursion Instead of for/while
Function Composition Instead of Attribute Mutation
Cool Things to Do with Pure Functions
comp
memoize
Peg Thing
Playing
Code Organization
Creating the Board
Moving Pegs
Rendering and Printing the Board
Player Interaction
Summary
Exercises
Chapter 6: Organizing Your Project: A Librarian’s Tale
Your Project as a Library
Storing Objects with def
Creating and Switching to Namespaces
refer
alias
Real Project Organization
The Relationship Between File Paths and Namespace Names
Requiring and Using Namespaces
The ns Macro
To Catch a Burglar
Summary
Chapter 7: Clojure Alchemy: Reading, Evaluation, and Macros
An Overview of Clojure’s Evaluation Model
The Reader
Reading
Reader Macros
The Evaluator
These Things Evaluate to Themselves
Symbols
Lists
Macros
Syntactic Abstraction and the -> Macro
Summary
Exercises
Chapter 8: Writing Macros
Macros Are Essential
Anatomy of a Macro
Building Lists for Evaluation
Distinguishing Symbols and Values
Simple Quoting
Syntax Quoting
Using Syntax Quoting in a Macro
Refactoring a Macro and Unquote Splicing
Things to Watch Out For
Variable Capture
Double Evaluation
Macros All the Way Down
Brews for the Brave and True
Validation Functions
if-valid
Summary
Exercises
Part III: Advanced Topics
Chapter 9: The Sacred Art of Concurrent and Parallel Programming
Concurrency and Parallelism Concepts
Managing Multiple Tasks vs. Executing Tasks Simultaneously
Blocking and Asynchronous Tasks
Concurrent Programming and Parallel Programming
Clojure Implementation: JVM Threads
What’s a Thread?
The Three Goblins: Reference Cells, Mutual Exclusion, and Dwarven Berserkers
Futures, Delays, and Promises
Futures
Delays
Promises
Rolling Your Own Queue
Summary
Exercises
Chapter 10: Clojure Metaphysics: Atoms, Refs, Vars, and Cuddle Zombies
Object-Oriented Metaphysics
Clojure Metaphysics
Atoms
Watches and Validators
Watches
Validators
Refs
Modeling Sock Transfers
commute
Vars
Dynamic Binding
Altering the Var Root
Stateless Concurrency and Parallelism with pmap
Summary
Exercises
Chapter 11: Mastering Concurrent Processes with core.async
Getting Started with Processes
Buffering
Blocking and Parking
thread
The Hot Dog Machine Process You’ve Been Longing For
alts!!
Queues
Escape Callback Hell with Process Pipelines
Additional Resources
Summary
Chapter 12: Working with the JVM
The JVM
Writing, Compiling, and Running a Java Program
Object-Oriented Programming in the World’s Tiniest Nutshell
Ahoy, World
Packages and Imports
JAR Files
clojure.jar
Clojure App JARs
Java Interop
Interop Syntax
Creating and Mutating Objects
Importing
Commonly Used Java Classes
The System Class
The Date Class
Files and Input/Output
Resources
Summary
Chapter 13: Creating and Extending Abstractions with Multimethods, Protocols, and Records
Polymorphism
Multimethods
Protocols
Records
Further Study
Summary
Exercises
Appendix A: Building and Developing with Leiningen
The Artifact Ecosystem
Identification
Dependencies
Plug-Ins
Summary
Appendix B: Boot, the Fancy Clojure Build Framework
Boot’s Abstractions
Tasks
The REPL
Composition and Coordination
Handlers and Middleware
Tasks Are Middleware Factories
Filesets
Next Steps
Farewell!
Index
Resources