Pages: 120
Published: August 2014
ISBN: 9781941222225
In Print
Level up your skills by taking advantage of Clojure’s powerful macro system. Macros make hard things possible and normal things easy. They can be tricky to use, and this book will help you deftly navigate the terrain. You’ll discover how to write straightforward code that avoids duplication and clarifies your intentions. You’ll learn how and why to write macros. You’ll learn to recognize situations when using a macro would (and wouldn’t!) be helpful. And you’ll use macros to remove unnecessary code and build new language features.
PDF for desktop/tablets
epub for Apple Books, e-readers
mobi for Kindle readers
Get all eBook formats here for $11.00 (USD)
Order via Bookshop (U.S. Only)
Please support indie bookstores!
Find indie bookstores in the U.S. Find indie bookstores around the world.
Clojure offers some sharp tools in its toolbox, and one of the sharpest is its macro system. This book will help you write macros using Clojure, and more importantly, recognize when you should be using macros in the first place.
The Lisp “code-as-data” philosophy gives tremendous advantages to macro authors and users. You can use macros to evaluate code in other contexts, move computations to compile time, and create beautiful API layers. You don’t need to wait on the Clojure language itself to add new features, you’ll learn how to implement even the lowest-level features as macros. You’ll step through representative samples of how to use macros in production libraries and applications, find clear details on how to construct macros, and learn pointers to avoid obstacles that often trip up macro amateurs.
Clojure macros are more straightforward to use than metaprogramming features in many other languages, but they’re different enough from normal programming to present challenges of their own. Mastering Clojure Macros examines some of these issues, along with alternatives to macros where they exist.
By the time you finish this book, you’ll be thinking like a macro professional.
The book examples have been developed under Clojure 1.6.0, although earlier and later versions of Clojure may work as well. You’ll want to use Leiningen 2.x in order to follow along with the examples that use external projects.
As Clojure programmers, we use macros all the time, even if we don’t write them ourselves. So understanding the details of how macros work is a time investment that pays off right away.
1. I already know some Clojure and have written some macros. Will this be review for me?
You actually sound like my target audience: someone who knows Clojure but is interested in digging a little deeper into macros. It’s hard for me to say for sure without being in your place, but there should be some interesting challenges for everyone. Take a look at the sample chapters and see what you think!
2. What’s a macro?
You can think of a macro as being sort of like a function, but one that transforms and replaces the code that it’s given at compile time. So it allows you to write code that generates other code: metaprogramming! And in Lisps, since the code is represented just like the data structures in the language, a lot of times it’s pretty similar to regular programming.
3. I’ve heard that you should avoid macros - why write a book about them?
Ah, right, so people say you shouldn’t use a macro where a function will do, which I’m 100% on board with. I think when you start out as a Clojure programmer, it’s easy to go overboard because (among other reasons) they can be sort of contagious. I talk about some of the problems with macros in Chapter 3, “Use Your Powers Wisely.” But back to the question: I actually learned a lot of what I know about macros from a couple of great macro-focused books, On Lisp and Let Over Lambda. And I figured it would’ve been nice to have had a book in Clojure, so I could focus on the ideas I wanted to learn instead of a new language.
4. I noticed a category of macro you left out of the book.
That’s not a question. Yes, unfortunately I didn’t have room to cover all the use cases I’ve seen, so there are a number of interesting use cases that I didn’t talk about. Send me an example and a note, though - I’m compiling some interesting macros at clojuremacros.com and I’d love to consider your macro idea for inclusion there! And if you’ve got any other questions, come on over to forums where I’m happy to discuss the book with you!
Releases:
PDF for desktop/tablets
epub for Apple Books, e-readers
mobi for Kindle readers
Get all eBook formats here for $11.00 (USD)
Order via Bookshop (U.S. Only)
Please support indie bookstores!
Find indie bookstores in the U.S. Find indie bookstores around the world.
Pages: 120
Published: August 2014
ISBN: 9781941222225
Edition: 1
In Print