O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.  Foreword
  2.  A Vain Attempt at a Justification
    1. Acknowledgments
  3. 1. Take 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. 2. Pattern Matching
      1. Assignment: 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. 3. Immutability
      1. You Already Have (Some) Immutable Data
      2. Immutable Data Is Known Data
      3. Performance Implications of Immutability
      4. Coding with Immutable Data
    3. 4. Elixir 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. 5. Anonymous 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. 6. Modules 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. 7. Lists 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. 8. Maps, 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. 9. An Aside—What Are Types?An Aside—What Are Types?
    9. 10. Processing Collections—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. 11. Strings 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. 12. Control Flow
      1. if and unless
      2. cond
      3. case
      4. Raising Exceptions
      5. Designing with Exceptions
      6. Doing More with Less
    12. 13. Organizing 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 n 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. 14. Working 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. 15. Nodes—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. 16. OTP: Servers
      1. Some OTP Definitions
      2. An OTP Server
      3. GenServer Callbacks
      4. Naming a Process
      5. Tidying Up the Interface
    4. 17. OTP: Supervisors
      1. Supervisors and Workers
      2. Supervisors Are the Heart of Reliability
    5. 18. OTP: 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. 19. Tasks and Agents
      1. Tasks
      2. Agents
      3. A Bigger Example
      4. Agents and Tasks, or GenServer?
  6. Part III. More-Advanced Elixir
    1. 20. Macros 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. 21. Linking Modules: Behavio(u)rs and Use
      1. Behaviours
      2. Use and __using__
      3. Putting It Together—Tracing Method Calls
      4. Use use
    3. 22. Protocols—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. 23. More Cool Stuff
      1. Writing Your Own Sigils
      2. Multi-app Umbrella Projects
      3. But Wait! There’s More!
  7. A1. Exceptions: 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. A2. Type 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.  Bibliography