Preface

Elixir offers developers the functional power and concurrent resilience of Erlang, with friendlier syntax, libraries, and metaprogramming. Elixir compiles to Erlang byte code, and you can mix and match it with Erlang and Erlang tools. Despite a shared foundation, however, Elixir feels very different, perhaps more similar to Ruby than to Erlang’s ancestor Prolog.

Introducing Elixir will give you a gentle guide to this powerful language.

Note

This release of Introducing Elixir covers version 1.0.0. We will update it as the language evolves. If you find mistakes or things that have broken, please let us know through the errata system.

Who This Book Is For

This book is mostly for people who’ve been programming in other languages but want to look around. Maybe you’re being very practical, and a distributed model, with its resulting scale and resilience advantages, appeals to you. Maybe you want to see what this “functional programming” stuff is all about. Or maybe you’re just going for a hike, taking your mind to a new place.

I suspect that functional programming is more approachable as a first language, before you’ve learned to program in other paradigms. However, getting started in Elixir—sometimes even just installing it—requires a fair amount of computing skill. If you’re a complete newcomer to programming, welcome, but there will be a few challenges along the way.

Who This Book Is Not For

This book is not for people in a hurry to get things done.

If you already know Elixir, you don’t likely need this book unless you’re looking for a slow brush-up.

If you already know Erlang, this book will give you an opportunity to see how things are different, but odds are good that you understand the key structures.

If you’re already familiar with functional languages, you may find the pacing of this gentle introduction hopelessly slow. Definitely feel welcome to jump to another book or online documentation that moves faster if you get bored.

What This Book Will Do For You

You’ll learn to write simple Elixir programs. You’ll understand why Elixir makes it easier to build resilient programs that can scale up and down with ease. You’ll be able to read other Elixir resources that assume a fair amount of experience and make sense of them.

In more theoretical terms, you’ll get to know functional programming. You’ll learn how to design programs around message passing and recursion, creating process-oriented programs focused more on data flow.

Most importantly, the gates to concurrent application development will be open. Though this introduction only gets you started using the incredible powers of OTP, that foundation can take you amazing places. Once you’ve mastered the syntax and learned about Elixir’s expectations for structuring programs, your next steps should be creating reliable and scalable applications - with much less effort than you would have needed in other approaches!

How This Book Works

This book tries to tell a story with Elixir. You’ll probably get the most out of it if you read it in order at least the first time, though you’re always welcome to come back to find whatever bits and pieces you need.

You’ll start by getting Elixir installed and running, and looking around its shell, IEx. You’ll spend a lot of time in that shell, so get cozy. Next, you’ll start loading code into the shell to make it easier to write programs, and you’ll learn how to call that code and mix it up.

You’ll take a close look at numbers because they’re an easy place to get familiar with Elixir’s basic structures. Then you’ll learn about atoms, pattern matching, and guards—the likely foundations of your program structure. After that you’ll learn about strings, lists, and the recursion at the heart of much Elixir processing. Once you’ve gone a few thousand recursions down and back, it’ll be time to look at processes, a key part of Elixir that relies on the message-passing model to support concurrency and resilience.

Once you have the foundation set, you can take a closer look at debugging and data storage, and then get a quick look at a toolset that is likely at the heart of your long-term development with Elixir: Erlang’s Open Telecom Platform (OTP), which is about much much more than telephones.

Finally, you’ll learn about Elixir’s macro tools, features that give Elixir tremendous flexibility by letting you extend the language.

Some people want to learn programming languages through a dictionary, smashing together a list of operators, control structures, and datatypes. Those lists are here, but they’re in Appendix A, not the main flow of the book.

The main point you should get from this book is that you can program in Elixir. If you don’t get that, let me know!

Other Resources

This book may not be the best way for you to learn Elixir. It all depends on what you want to learn and why. If you’re looking for a faster-flying introduction to the language, Dave Thomas’ Programming Elixir (Pragmatic Publishers) jumps in faster and emphasizes Elixir’s uniqueness more frequently.

If you like the pace of this book and want to try out your new knowledge, you might like Études for Elixir (O’Reilly Media). That book provides descriptions of short programs that you can write in Elixir, and they may ask you stretch a bit beyond the examples you find here. It is also designed so that its chapters are in parallel with this book’s chapters.

The other books in the field all cover Erlang, not Elixir. Hopefully there will be more Elixir-specific work soon. Elixir in Action (Manning) is getting underway. The main Elixir website includes a lot of tutorials, documentation, and links to other resources.

If your primary interest in learning Elixir is to break out of a programming rut, you should explore Bruce Tate’s wild tour of Seven Languages in Seven Weeks (Pragmatic Publishers), which explores Ruby, Io, Prolog, Scala, Erlang, Clojure, and Haskell. Erlang gets only (an excellent) 37 pages, but that might be what you want.

Erlang books can also help you understand what makes Elixir work so well.

For a simple introduction to Erlang that largely parallels this book, Introducing Erlang will get you started with Erlang and functional programming.

For an online experience (now also in print from No Starch Books) with more snark and funnier illustrations, you should explore Fred Hebert’s Learn You Some Erlang for Great Good!.

The two classic general books on Erlang are the similarly-titled Programming Erlang (Pragmatic Publishers) by Erlang creator Joe Armstrong, and Erlang Programming (O’Reilly) by Francesco Cesarini and Simon Thompson. They cover a lot of similar and overlapping terrain, and both may be good places to start if this book moves too slowly or you need more reference material. Erlang Programming goes further into what you can do with Erlang, whereas Programming Erlang provides a lot of detail on setting up an Erlang programming environment.

On the more advanced side, Erlang and OTP in Action (Manning) by Martin Logan, Eric Merritt, and Richard Carlsson, opens with a high-speed 72-page introduction to Erlang and then spends most of its time applying the Open Telecom Platform, Erlang’s framework for building upgradeable and maintainable concurrent applications.

Designing for Scalability with Erlang/OTP (O’Reilly), by Francesco Cesarini and Steve Vinoski, explores how OTP and Erlang make things that seem hugely difficult in other environments a normal day’s work in Erlang.

If you want to focus on connecting Erlang to the Web, you should definitely also explore Building Erlang Web Applications (O’Reilly) by Zachary Kessin.

You’ll also want to visit the main Erlang website for updates, downloads, documentation, and more.

Elixir Will Change You

Before you go deeper, you should know that working in Elixir may irrevocably change the way you look at programs. Its combination of functional code, process orientation, and distributed development may seem alien at first. However, once it sinks in, Elixir can transform the way you solve problems (perhaps even beyond the way Erlang does), and potentially make it difficult to return to other languages, environments, and programming cultures.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Note

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

The examples in this book are meant to teach basic concepts in small bites, making it easy to see what changed from one example to another. While you may certainly borrow code and reuse it as you see fit, you won’t be able to take the code of this book and build a stupendous application instantly (unless perhaps you have an unusual fondness for calculating the speeds of falling objects). You should, however, be able to figure out the steps you need to take to build a great application.

You can download the code from GitHub. (Eventually it will also be available from the Examples link on the book’s catalog page.)

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you are reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Introducing Elixir, by Simon St.Laurent and J. David Eisenberg (O’Reilly). Copyright 2014 Simon St.Laurent and J. David Eisenberg, 978-1-449-36999-6.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.

Help This Book Grow

While I hope that you will enjoy reading this book and learn from it, I also hope that you can contribute to helping other readers learn Elixir here. You can help your fellow readers in a number of ways:

  • If you find specific technical problems, bad explanations, or things that can be improved, please report them through the errata system.

  • If you like (or don’t like) the book, please leave reviews. The most visible places to do so are on Amazon.com (or its international sites) and at the O’Reilly page for the book. Detailed explanations of what worked and what didn’t work for you (and the broader target audience of programmers new to Erlang) are helpful to other readers and to me.

  • If you find you have much more you want to say about Elixir, please consider sharing it, whether on the Web, in a book of your own, in training classes, or in whatever form you find easiest.

We’ll update the book for errata and try to address issues raised in reviews. Even once the book is “complete,” I may still add some extra pieces to it. If you purchased it as an ebook, you’ll receive these updates for free at least up to the point where it’s time for a whole new edition. I don’t expect that new edition declaration to come quickly, however, unless the Elixir world changes substantially.

Hopefully this book will engage you enough to make you consider sharing.

Please Use It For Good

We’ll let you determine what “good” means, but think about it. Please try to use Elixir’s power for projects that make the world a better place, or at least not a worse place.

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/introducing_elixir.

To comment or ask technical questions about this book, send email to bookquestions@oreilly.com.

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

The Elixir community is amazing, open to questions and suggestions from a wide range of perspectives. We’ve been lucky to be able to ask questions and get them answered, and have enjoyed a rare community that treats “difficult to explain” as a problem worth fixing in code.

José Valim’s leadership and explanations have helped us throughout the project. Our competitor Dave Thomas confirmed that yes, Elixir is here and the world is waiting for it. From the Erlang side, Francesco Cesarini encouraged us to purse this new language sibling. Reviewers Bibek Pandey, Alexei Sholik, David Lorenzetti, Bengt Kleberg, Mistral Contrastin, Augie De Blieck Jr, Arie van Wingerden, Elias Carrillo, and Nicholas helped us find the errors of our ways.

Our editor Meghan Blanchette kept us on track, and Melanie Yarbrough saw the book through an intricate production process as we waited for Elixir to finalize.

Also, J. David Eisenberg’s commitment to the project saved Simon St.Laurent repeatedly!

Thanks also to Simon, who made David’s first experience as a co-author a pleasant one.

Get Introducing Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.