small medium large xlarge

Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)


Cover image for Metaprogramming Elixir

Metaprogramming Elixir

Write Less Code, Get More Done (and Have Fun!)


Write code that writes code with Elixir macros. Macros make metaprogramming possible and define the language itself. In this book, you’ll learn how to use macros to extend the language with fast, maintainable code and share functionality in ways you never thought possible. You’ll discover how to extend Elixir with your own first-class features, optimize performance, and create domain-specific languages.

Customer Reviews

This book is exactly what the young Elixir community needs! Chris McCord does
an elegant job of laying out Elixir metaprogramming step by step, with practical
and wonderfully instructive examples throughout.

- Bruce Tate

President, RapidRed, LLC

Whether you’re new to Elixir or a seasoned pro, this compact book will give you
the foundation you need to harness the full power of Elixir. A joy to read as it
gently walks the reader toward metaprogramming mastery, it’s a thoughtful and
practical guide to metaprogramming you’ll want to visit again and again.

- Matt Sears

CEO, Littlelines

Chris is the person to be writing this book; reading his work in open source is
how I learned how to use macros. This book filled in the gaps of my understanding
and improved my intuition for how Elixir the language works.

- Jason Stiebs

Partner, RokkinCat

Metaprogramming Elixir made me want to run out and write code that writes code
for me! Great voice and compelling examples!

- Zander Hill

, Polyglot

Chris has a habit of seeing past the surface of a technology. In Metaprogramming Elixir, Chris demystifies the foundation of Elixir itself, opening the door for every Elixir programmer to build applications in fun, powerful ways.

- Ryan Cromwell

Technical director, Sparkbox

A treasure trove of metaprogramming patterns, this book is just what the community
needs to communicate the power, extensibility, and practicality of metaprogramming
in Elixir. After reading it, you’ll know how and why to use metaprogramming
both responsibly and irresponsibly. Definitely a must-have for anyone
wanting to go beyond the basics of a beautiful language.

- Gabriel Jaldon

Web developer, open source enthusiast

See All Reviews

Choose Your Format(s)

  • $11.00 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

What You Need

You’ll need a computer, Elixir 1.x, and have a solid grasp of Elixir basics.

Contents & Extracts

  • Acknowledgements
  • Introduction
    • Metaprogramming—An Essential Feature
    • How to Read This Book
    • Running the Code Exercises
    • Online Resources
  • The Language of Macros excerpt
    • The World Is Your Playground
    • Macro Rules
    • The Abstract Syntax Tree—Demystified
    • Macros: The Building Blocks of Elixir
    • Code Injection and the Caller’s Context
    • Further Exploration
  • Extending Elixir with Metaprogramming
    • Custom Language Constructs
    • Smarter Testing with Macros
    • Extending Modules
    • Using Module Attributes for Code Generation
    • Compile-Time Hooks
    • Further Exploration
  • Advanced Compile-Time Code Generation
    • Generating Functions from External Data
    • MIME-Type Conversion in Ten Lines of Code
    • Building an Internationalization Library excerpt
    • Code Generation from Remote APIs
    • Further Exploration
  • How to Test Macros
    • Setting Up Your Test Suite
    • Deciding What to Test
    • Integration Testing
    • Unit Tests
    • Test Simple and Test Fast
    • Further Exploration
  • Creating an HTML Domain-Specific Language
    • Getting Domain Specific
    • Start by Defining the Minimum Viable API
    • Support the Entire HTML Spec with Macros
    • Enhance Your API with HTML Attribute Support
    • Generate Less Code by Walking the AST
    • To DSL or Not to DSL?
    • Further Exploration
  • With Great Power Comes Great Responsibility (and Fun!)
    • When and Where to Use Macros
    • Avoiding Common Pitfalls
    • Bending the Rules
    • Build the Future


Chris McCord is a programmer with a passion for science and building things. He spends his free time crafting the Phoenix Web Framework, speaking at conferences, and teaching others the tools of the trade. He loves community outreach and helping to grow Elixir’s wonderful community.