You are previewing Programming Elixir 1.2.
O'Reilly logo
Programming Elixir 1.2

Book Description

You want to explore functional programming, but are put off by the academic feel (tell me about monads just one more time). You know you need concurrent applications, but also know these are almost impossible to get right. Meet Elixir, a functional, concurrent language built on the rock-solid Erlang VM. Elixir's pragmatic syntax and built-in support for metaprogramming will make you productive and keep you interested for the long haul. This book is the introduction to Elixir for experienced programmers.

Maybe you need something that's closer to Ruby, but with a battle-proven environment that's unrivaled for massive scalability, concurrency, distribution, and fault tolerance. Maybe the time is right for the Next Big Thing. Maybe it's Elixir.

This edition of the book has been updated to cover Elixir 1.2, including the new with expression, the exrm release manager, and the removal of deprecated types.

Table of Contents

  1. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">&#160;</span>ForewordForeword
  2. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">&#160;</span>A Vain Attempt at a JustificationA Vain Attempt at a Justification
    1. Acknowledgments
  3. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">1. </span>Take the Red PillTake the Red Pill
    1. Programming Should Be About Transforming Data
    2. Installing Elixir
    3. Running Elixir
    4. Suggestions for Reading the Book
    5. Exercises
    6. Think Different(ly)
  4. Part I. Conventional Programming
    1. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">2. </span>Pattern MatchingPattern Matching
      1. Assignment: <br xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"></br>I Do Not Think It Means What You Think It Means.I Do Not Think It Means What You Think It Means.
      2. More Complex Matches
      3. Ignoring a Value with _ (Underscore)
      4. Variables Bind Once (per Match)
      5. Another Way of Looking at the Equals Sign
    2. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">3. </span>ImmutabilityImmutability
      1. You Already Have (Some) Immutable Data
      2. Immutable Data Is Known Data
      3. Performance Implications of Immutability
      4. Coding with Immutable Data
    3. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">4. </span>Elixir BasicsElixir Basics
      1. Built-in Types
      2. Value Types
      3. System Types
      4. Collection Types
      5. Maps
      6. Names, Source Files, Conventions, Operators, and So On
      7. Variable Scope
      8. End of the Basics
    4. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">5. </span>Anonymous FunctionsAnonymous Functions
      1. Functions and Pattern Matching
      2. One Function, Multiple Bodies
      3. Functions Can Return Functions
      4. Passing Functions As Arguments
      5. Functions Are the Core
    5. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">6. </span>Modules and Named FunctionsModules and Named Functions
      1. Compiling a Module
      2. The Function’s Body Is a Block
      3. Function Calls and Pattern Matching
      4. Guard Clauses
      5. Default Parameters
      6. Private Functions
      7. The Amazing Pipe Operator: |>
      8. Modules
      9. Module Attributes
      10. Module Names: Elixir, Erlang, and Atoms
      11. Calling a Function in an Erlang Library
      12. Finding Libraries
    6. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">7. </span>Lists and RecursionLists and Recursion
      1. Heads and Tails
      2. Using Head and Tail to Process a List
      3. Using Head and Tail to Build a List
      4. Creating a Map Function
      5. Keeping Track of Values During Recursion
      6. More Complex List Patterns
      7. The List Module in Action
      8. Get Friendly with Lists
    7. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">8. </span>Maps, Keyword Lists, Sets, and StructsMaps, Keyword Lists, Sets, and Structs
      1. How to Choose Between Maps and Keyword Lists
      2. Keyword Lists
      3. Maps
      4. Pattern Matching and Updating Maps
      5. Updating a Map
      6. Structs
      7. Nested Dictionary Structures
      8. Sets
      9. With Great Power Comes Great Temptation
    8. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">9. </span>An Aside&#8212;What Are Types?An Aside—What Are Types?
    9. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">10. </span>Processing Collections&#8212;Enum and StreamProcessing Collections—Enum and Stream
      1. Enum—Processing Collections
      2. Streams—Lazy Enumerables
      3. The Collectable Protocol
      4. Comprehensions
      5. Moving Past Divinity
    10. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">11. </span>Strings and BinariesStrings and Binaries
      1. String Literals
      2. The Name “strings”
      3. Single-Quoted Strings—Lists of Character Codes
      4. Binaries
      5. Double-Quoted Strings Are Binaries
      6. Binaries and Pattern Matching
      7. Familiar Yet Strange
    11. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">12. </span>Control FlowControl Flow
      1. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="cf ic">if</span> and and <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="cf ic">unless</span>
      2. Raising Exceptions
      3. Designing with Exceptions
      4. Doing More with Less
    12. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">13. </span>Organizing a ProjectOrganizing a Project
      1. The Project: Fetch Issues from GitHub
      2. Task: Use Mix to Create Our New Project
      3. Transformation: Parse the Command Line
      4. Step: Write Some Basic Tests
      5. Transformation: Fetch from GitHub
      6. Task: Use Libraries
      7. Transformation: Convert Response
      8. Transformation: Sort Data
      9. Transformation: Take First <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="emph">n</span> Items Items
      10. Transformation: Format the Table
      11. Task: Make a Command-Line Executable
      12. Task: Add Some Logging
      13. Task: Test the Comments
      14. Task: Create Project Documentation
      15. Coding by Transforming Data
  5. Part II. Concurrent Programming
    1. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">14. </span>Working with Multiple ProcessesWorking with Multiple Processes
      1. A Simple Process
      2. Process Overhead
      3. When Processes Die
      4. Parallel Map—The “Hello, World” of Erlang
      5. A Fibonacci Server
      6. Agents—A Teaser
      7. Thinking in Processes
    2. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">15. </span>Nodes&#8212;The Key to Distributing ServicesNodes—The Key to Distributing Services
      1. Naming Nodes
      2. Naming Your Processes
      3. I/O, PIDs, and Nodes
      4. Nodes Are the Basis of Distribution
    3. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">16. </span>OTP: ServersOTP: Servers
      1. Some OTP Definitions
      2. An OTP Server
      3. GenServer Callbacks
      4. Naming a Process
      5. Tidying Up the Interface
    4. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">17. </span>OTP: SupervisorsOTP: Supervisors
      1. Supervisors and Workers
      2. Supervisors Are the Heart of Reliability
    5. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">18. </span>OTP: ApplicationsOTP: Applications
      1. This Is Not Your Father’s Application
      2. The Application Specification File
      3. Turning Our Sequence Program into an OTP Application
      4. Supervision Is the Basis of Reliability
      5. Releasing Your Code
      6. EXRM—the Elixir Release Manager
      7. OTP Is Big—Unbelievably Big
    6. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">19. </span>Tasks and AgentsTasks and Agents
      1. Tasks
      2. Agents
      3. A Bigger Example
      4. Agents and Tasks, or GenServer?
  6. Part III. More-Advanced Elixir
    1. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">20. </span>Macros and Code EvaluationMacros and Code Evaluation
      1. Implementing an if Statement
      2. Macros Inject Code
      3. Using the Representation As Code
      4. Using Bindings to Inject Values
      5. Macros Are Hygienic
      6. Other Ways to Run Code Fragments
      7. Macros and Operators
      8. Digging Deeper
      9. Digging Ridiculously Deep
    2. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">21. </span>Linking Modules: Behavio(u)rs and UseLinking Modules: Behavio(u)rs and Use
      1. Behaviours
      2. Use and <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="cf ic">__using__</span>
      3. Putting It Together—Tracing Method Calls
      4. Use <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="cf ic">use</span>
    3. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">22. </span>Protocols&#8212;Polymorphic FunctionsProtocols—Polymorphic Functions
      1. Defining a Protocol
      2. Implementing a Protocol
      3. The Available Types
      4. Protocols and Structs
      5. Build-In Protocols
      6. Protocols Are Polymorphism
    4. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">23. </span>More Cool StuffMore Cool Stuff
      1. Writing Your Own Sigils
      2. Multi-app Umbrella Projects
      3. But Wait! There’s More!
  7. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">A1. </span>Exceptions: raise and try, catch and throwExceptions: raise and try, catch and throw
    1. Raising an Exception
    2. catch, exit, and throw
    3. Defining Your Own Exceptions
    4. Now Ignore This Appendix
  8. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">A2. </span>Type Specifications and Type CheckingType Specifications and Type Checking
    1. When Specifications Are Used
    2. Specifying a Type
    3. Defining New Types
    4. Specs for Functions and Callbacks
    5. Using Dialyzer
  9. <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="toc-secnum">&#160;</span>BibliographyBibliography