Introducing Elixir Getting Started in Functional Programming 2nd Edition Edition Simon St. Laurent instant download
Introducing Elixir Getting Started in Functional Programming 2nd Edition Edition Simon St. Laurent instant download
https://ebookfinal.com/download/introducing-elixir-getting-
started-in-functional-programming-2nd-edition-edition-simon-st-
laurent/
https://ebookfinal.com/download/introducing-elixir-getting-started-in-
functional-programming-1st-edition-simon-st-laurent/
https://ebookfinal.com/download/from-ruby-to-elixir-unleash-the-full-
potential-of-functional-programming-1st-edition-stephen-bussey/
https://ebookfinal.com/download/getting-started-in-hedge-funds-2nd-ed-
edition-daniel-a-strachman/
https://ebookfinal.com/download/getting-started-with-c-audio-
programming-for-game-development-1st-edition-david-gouveia/
Getting Started With Advanced C Upgrade Your Programming
Skills 1st Edition Vaskaran Sarcar
https://ebookfinal.com/download/getting-started-with-advanced-c-
upgrade-your-programming-skills-1st-edition-vaskaran-sarcar/
https://ebookfinal.com/download/make-getting-started-with-
processing-2nd-edition-edition-fry/
https://ebookfinal.com/download/getting-started-in-options-michael-c-
thomsett/
https://ebookfinal.com/download/getting-started-in-investment-
analysis-warren-brussee/
https://ebookfinal.com/download/getting-started-in-options-7th-
edition-michael-c-thomsett/
Introducing Elixir Getting Started in Functional
Programming 2nd Edition Edition Simon St. Laurent
Digital Instant Download
Author(s): Simon St. Laurent, J. David Eisenberg
ISBN(s): 9781491956748, 1491956747
Edition: 2nd Edition
File Details: PDF, 11.18 MB
Year: 2016
Language: english
Introducing Elixir
Boston
Introducing Elixir
by Simon St. Laurent and J. David Eisenberg
Copyright © 2016 Simon St. Laurent and J. Eisenberg. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles ( http://safaribooksonline.com ). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com .
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Introducing Elixir, the cover image,
and related trade dress are trademarks of O’Reilly Media, Inc.
While the publisher and the author(s) have used good faith efforts to ensure that the information and
instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibil‐
ity for errors or omissions, including without limitation responsibility for damages resulting from the use
of or reliance on this work. Use of the information and instructions contained in this work is at your own
risk. If any code samples or other technology this work contains or describes is subject to open source
licenses or the intellectual property rights of others, it is your responsibility to ensure that your use
thereof complies with such licenses and/or rights.
978-1-491-95674-8
[FILL IN]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Getting Comfortable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Installation 1
Installing Erlang 1
Installing Elixir 2
Firing It Up 2
First Steps 3
Moving Through Text and History 4
Moving Through Files 4
Doing Something 5
Calling Functions 6
Numbers in Elixir 7
Working with Variables in the Shell 9
iii
3. Atoms, Tuples, and Pattern Matching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Atoms 27
Pattern Matching with Atoms 27
Atomic Booleans 29
Guards 30
Underscoring That You Don’t Care 33
Adding Structure: Tuples 35
Pattern Matching with Tuples 36
Processing Tuples 37
6. Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
List Basics 65
Splitting Lists into Heads and Tails 67
Processing List Content 68
Creating Lists with Heads and Tails 70
Mixing Lists and Tuples 72
Building a List of Lists 72
7. Name-Value Pairs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Keyword Lists 77
iv | Table of Contents
Lists of Tuples with Multiple Keys 79
Hash Dictionaries 80
From Lists to Maps 81
Creating Maps 81
Updating Maps 82
Reading Maps 82
From Maps to Structs 82
Setting Up Structs 83
Creating and Reading Structs 83
Pattern Matching Against Structs 84
Using Structs in Functions 84
Adding Behavior to Structs 87
Adding to Existing Protocols 88
Table of Contents | v
11. Static Analysis, Typespecs, and Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Static Analysis 131
Typespecs 133
Writing Unit Tests 136
Setting up Tests 139
e. Embedding Tests in Documentation
vi | Table of Contents
A Simple View 196
Calculating 198
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
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.
ix
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.
x | Preface
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 lan‐
guage, 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 pro‐
grams 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!.
Preface | xi
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 applica‐
tions.
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, documenta‐
tion, and more.
xii | Preface
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.
Preface | xiii
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 fel‐
low 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.
Technology professionals, software developers, web designers, and business and crea‐
tive 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.
xiv | Preface
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 Kauf‐
mann, 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:
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 bookques‐
tions@oreilly.com.
For more information about our books, courses, conferences, and news, see our web‐
site 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 lan‐
guage sibling. Reviewers Bibek Pandey, Alexei Sholik, David Lorenzetti, Bengt Kle‐
Preface | xv
berg, 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 repeat‐
edly!
Thanks also to Simon, who made David’s first experience as a co-author a pleasant
one.
xvi | Preface
CHAPTER 1
Getting Comfortable
The easiest place to start learning Elixir is in Interactive Elixir, iex. This command-
line interface is a cozy place to get started and a good place to start figuring out what
works and what doesn’t work in Elixir. Its features will spare you headaches later, so
settle in!
Installation
Because Elixir runs on top of Erlang, you’ll need to install Erlang on your system first,
and then install Elixir.
Installing Erlang
If you’re on Windows, installing Erlang is easy. Download the Windows binary file,
run the installer, and you’re set. If you are a brave beginner tackling your first pro‐
gramming language, this is easily your best bet.
On Linux or Mac OS X, you may be able to download the source file and compile it.
For me, on Mac OS X, I just had to unzip and untar it, and then, from the directory
created by the untarring, run ./configure, make, and sudo make install. However,
that simple sequence works only if you have the right files previously installed, and
can give you mysterious errors if they weren’t. In particular, Apple’s shift to the LLVM
compiler in newer versions of XCode instead of GCC makes it less likely that GCC
will be on newer Mac OS X systems, and Erlang needs GCC.
(You can also ignore the error about FOP, which Erlang uses to generate PDF docu‐
mentation you can download elsewhere. Also, on newer Macs, you’ll get an error at
the end that wxWidgets doesn’t work on 64-bit Mac OS X. For now, ignore this.)
1
If the compilation approach doesn’t work or isn’t for you, Erlang Solutions offers a
number of installs. Also, many different package managers (Debian, Ubuntu, Mac‐
Ports, homebrew, and so on) include Erlang. It may not be the very latest version, but
having Erlang running is much better than not having Erlang running. They do tend
to make it run on the latest version of various operating systems, so if you have instal‐
lation problems, look closely at their requirements.
Installing Elixir
Once you have Erlang installed, you should be able to download a precompiled ver‐
sion of Elixir or the GitHub source. Some package managers are starting to support
Elixir, including homebrew. This version of this book should work with Elixir 1.0.0.
Then you need to set your path so that it can find elixir/bin.
Elixir’s instructions for setup are organized into a tutorial.
Firing It Up
Go to the command line (or shell, or terminal) and type mix new first_app. This
will invoke Elixir’s Mix tool, which “provides tasks for creating, compiling, and test‐
ing Elixir projects, managing its dependencies, and more.” In this case, the command
you typed creates a new, empty project in a directory named first_app:
$ mix new first_app
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/first_app.ex
* creating test
* creating test/test_helper.exs
* creating test/first_app_test.exs
cd first_app
mix test
224 Hardwicke, vol. 1., p. 141; who adds this note: “As Burnet,
in his Memoirs of Hamilton, has already mentioned, though in an
inaccurate way, this extraordinary warrant, it is thought not
improper to publish it exactly from the original.”—Burnet’s
statement thus referred to is to the following effect (p. 148:)
—“But, before they came to Berwick, the King ordered the
Marquis, by a warrant in writing, yet extant under His Majesties
hand, to try what way he could gain upon them, and discover the
bottom of their intentions, how the estate of Bishops should be
supplied in Parliament, and how far they intended to lessen the
King’s Authority. The King also allowed him to use what means he
pleased, and speak to them what he thought fit; not onely
authorizing, but requiring him to it, and warranting him, if he
were ever questioned or accused for it by any. Bearing date at
Berwick the 17th of July 1639.”
243 Rush vol. iii. p. 992, 1016, et sequen. Vide also Franklyn, p.
796, et sequen; Clarendon, and others.
312 Lanerick.
327 Vide Rushworth, vol. iv., part ii., p. 499; and Clarendon, vol.
ii., part i., p. 383.
334 Vide Neal’s Hist. of Puritans, vol. iii., p. 131. Baillie, vol. ii.,
p. 85
335 Rushworth, vol. i., p. 268, 271.
345 Ibid.
355 Rushworth, part iv., vol. i., p. 448. Thurloe, vol. i., p. 89,
92. Salmanet, p. 253-4.
356
The fire in the cavern of Etna concealed,
Still mantles unseen in its secret recess,
At length in a volume terrific revealed,
No torrent can quench it, no bounds can repress.
Byron.
358 Not that they are to be heer Printed, but because they
being to bee Printed severally, this act is to be prefixed to them.
365 Rushworth, part iv., vol. ii., p. 768 to 771. Acts of Estates.
373 Burnet, p. 323-334. Vide also Rushworth, part iv., vol. ii., p.
946-950 et passim.
386 Burnet, p. 348, et seq. Rushworth, part iv., vol. ii., p. 1193-
1242. Turner, p.63.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com