Programming Elixir 1.6
Functional |> Concurrent |> Pragmatic |> Fun
by Dave Thomas
Version: P1.0 (May 2018)
Copyright 2018 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/elixir16, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Andy Hunt (Publisher) Janet Furlow (VP of Operations) Brian MacDonald (Managing Editor) Jacquelyn Carter (Supervising Editor) Candace Cunningham (Copy Editor) Potomac Indexing, LLC (Indexing) Gilson Graphics (Layout)
For customer support, please contact .
For international rights, please contact .
Table of Contents
- Assignment:
I Do Not Think It Means What You Think It Means.
Copyright 2018, The Pragmatic Bookshelf.
Foreword
I have always been fascinated with how changes in hardware affect howwe write software.
A couple of decades ago, memory was a very limited resource. It madesense back then for our software to take hold of some piece ofmemory and mutate it as necessary. However, allocating this memory andcleaning up after we no longer needed it was a very error-pronetask. Some memory was never freed; sometimes memory was allocated overanother structure, leading to faults. At the time, garbage collectionwas a known technique, but we needed faster CPUs in order to use it inour daily software and free ourselves from manual memorymanagement. That has happenedmost of our languages are nowgarbage-collected.
Today, a similar phenomenon is happening. Our CPUs are not getting anyfaster. Instead, our computers get more and more cores. This means new software needs to use as many cores as it can if it is to maximizeits use of the machine. This conflicts directly with how we currently writesoftware.
In fact, mutating our memory state actually slows down our softwarewhen many cores are involved. If you have four cores trying to accessand manipulate the same piece of memory, they can trip over eachother. This potentially corrupts memory unless some kind ofsynchronization is applied.
I quickly learned that applying this synchronization is manual, errorprone, and tiresome, and it hurts performance. I suddenly realized thats nothow I wanted to spend time writing software in the next years of mycareer, and I set out to study new languages and technologies.
It was on this quest that I fell in love with the Erlang virtualmachine and ecosystem.
In the Erlang VM, all code runs in tiny concurrent processes, each with its own state. Processes talk to each othervia messages. And since all communication happens by message-passing,exchanging messages between different machines on the same network ishandled transparently by the VM, making it a perfect environment forbuilding distributed software!
However, I felt there was still a gap in the Erlang ecosystem. I missedfirst-class support for some of the features I find necessary in mydaily workthings such as metaprogramming, polymorphism, andfirst-class tooling. From this need, Elixir was born.
Elixir is a pragmatic approach to functional programming. It valuesits functional foundations and it focuses on developerproductivity. Concurrency is the backbone of Elixir software. Asgarbage collection once freed developers from the shackles of memorymanagement, Elixir is here to free you from antiquated concurrencymechanisms and bring you joy when writing concurrent code.
A functional programming language lets us think in terms of functionsthat transform data. This transformation never mutates data. Instead,each application of a function potentially creates a new, freshversion of the data. This greatly reduces the need fordata-synchronization mechanisms.
Elixir also empowers developers by providing macros. Elixir code isnothing more than data, and therefore can be manipulated via macros likeany other value in the language.
Finally, object-oriented programmers will find many of themechanisms they consider essential to writing good software, such aspolymorphism, in Elixir.
All this is powered by the Erlang VM, a 20-year-old virtual machinebuilt from scratch to support robust, concurrent, and distributedsoftware. Elixir and the Erlang VM are going to change how you writesoftware and make you ready to tackle the upcoming years inprogramming.
Jos Valim
Creator of Elixir
Tenczynek, Poland, October 2014
Copyright 2018, The Pragmatic Bookshelf.
A Vain Attempt at a Justification, Take Two